Skip to content Skip to sidebar Skip to footer

How To Create An In-memory Database With Schema Based On An Existing File Database

I have an existing database which structure is used accross the whole application. Instances of the databases are periodically rotated. I have a database file template.sqlite which

Solution 1:

You could use the .dump command of the command-line shell do create a bunch of SQL commands that you can execute.

Alternatively, you can use the backup API to copy the template into a new database.

Solution 2:

As a quick solution for all seeking answer to this question: a bit of C code that does the trick:

intloadOrSaveDb(sqlite3 *pInMemory, constchar *zFilename){
  int rc;
  sqlite3 *pFile;           /* Database connection opened on zFilename */
  sqlite3_backup *pBackup;  /* Backup object used to copy data */

  rc = sqlite3_open(zFilename, &pFile);
  if( rc==SQLITE_OK ) {

    pBackup = sqlite3_backup_init(pInMemory, "main", pFile, "main");
    if( pBackup ){
      (void)sqlite3_backup_step(pBackup, -1);
      (void)sqlite3_backup_finish(pBackup);
    }
    rc = sqlite3_errcode(pTo);
  }

  (void)sqlite3_close(pFile);
  return rc;
}

Post a Comment for "How To Create An In-memory Database With Schema Based On An Existing File Database"