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.sql
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;
}