How to create an in-memory database with schema based on an existing file database

旧巷老猫 提交于 2019-12-02 02:15:40

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.

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

int loadOrSaveDb(sqlite3 *pInMemory, const char *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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!