[yunqa.de] Re: Loading and Saving In-Memory Databases

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Tue, 28 Jun 2011 08:51:44 +0200

On 27.06.2011 19:19, edoardo falzetti wrote:

> At the end the user compares the obtained results and may save in the local
> sqlite3 db the best "parameters" and send to the corporate database the
> results.
> 
> What i would like to keep in the "in memory" DBs (two DBs) is:
> - the results (which at the end are not all saved and in case not in the local
> DB)
> - what i've been calling "parameters" which are some sql tables.

Create an in-memory database (named ':memory:') and store your results
and parameters there. This is fast and volatile.

> So, only the tables of the "parameters" may (or may not) be saved at program
> close.

Are you asking how to save selected data from an in-memory database to
an on-disk database?

Suppose you already have your in-memory database created. Next create
your on-disk database and ATTACH it to your in-memory database. Then
transfer data by issuing simple SQL commands like this:

CREATE TABLE disk.t AS SELECT * FROM main.t;

In the above example, "main" is the in-memory database and "disk" is the
on-disk database.

> The "parameters" table are not large but  i fear my cache would be mostly used
> by the results DB. (Question: do all the in  memory DB need to have the same
> "cache size" ?)

Memory databases do not have a cache size because they store all their
database pages in memory anyway. Or, in other words, memory databases
have an unlimited cache size and just do not write their cache to disk.

> I had already seen the BackUp demo but i was asking help because in the Sqlite
> documentation "Using the SQLite Online Backup API" the procedures
> "loadOrSaveDb" and "backupDb" are very similar but with differences not 
> limited
> to swapping source. As i do not know C i was fearing to risk to miss something
> trying to convert such code by myself.

Both functions in the documentation do more or less the same thing. I
have ported them to the demos DISQLite3_Backup.dpr and
DISQLite3_Backup_Memory.dpr, both in the
DISQLite3\Demos\DISQLite3_Backup\ folder.

I suggest you to use the following function from
DISQLite3_Backup_Memory.dpr. It backs up one database to another, both
by database handle. This allows to load or save an on-disk database to
an in-memory database and vice versa:

function backupDb(
  const DbSource: sqlite3_ptr; { Database handle to read from. }
  const DbDest: sqlite3_ptr; { Database handle to write to. }
  const AProgress: TBackupProgressFunc = nil; { Progress function to
invoke. }
  const BackupStepPages: Integer = BACKUP_STEP_PAGES;
  const SleepMS: Integer = BACKUP_SLEEP_MS
  ): Integer;

Ralf
_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: