Skip to content

Instantly share code, notes, and snippets.

@MXP2095onetechguy
Last active March 10, 2023 08:21
Show Gist options
  • Save MXP2095onetechguy/438b897c91d575b2f883677f3a2c7640 to your computer and use it in GitHub Desktop.
Save MXP2095onetechguy/438b897c91d575b2f883677f3a2c7640 to your computer and use it in GitHub Desktop.
Copy Sqlite databases. Ripped from https://www.sqlite.org/backup.html
int CopyDatabase(sqlite3 *pDb1, sqlite3* pDb2){ /* Copy from pDb1 to pDb2 */
int rc; /* Function return code */
sqlite3 *pFile; /* Database connection opened on zFilename */
sqlite3_backup *pBackup; /* Backup object used to copy data */
/* Set up the backup procedure to copy from the "main" database of
** connection pFile to the main database of connection pInMemory.
** If something goes wrong, pBackup will be set to NULL and an error
** code and message left in connection pTo.
**
** If the backup object is successfully created, call backup_step()
** to copy data from pFile to pInMemory. Then call backup_finish()
** to release resources associated with the pBackup object. If an
** error occurred, then an error code and message will be left in
** connection pTo. If no error occurred, then the error code belonging
** to pTo is set to SQLITE_OK.
*/
pBackup = sqlite3_backup_init(pDb1, "main", pDb2, "main");
if( pBackup ){
(void)sqlite3_backup_step(pBackup, -1);
(void)sqlite3_backup_finish(pBackup);
}
rc = sqlite3_errcode(pTo);
/* Close the database connection opened on database file zFilename
** and return the result of this function. */
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment