SQLite3 backup and restore (sync) DBs [sqlite3_backup]

/* isSave => 0 : for backup to current [Restore]
*                      1 : For current to backup [Backup]
*/
int
ji_sync_db (sqlite3 *currentDb, const char *backupDbName, int isSave)
{
int rc;
sqlite3 *pFile;
sqlite3_backup *pBackup;
sqlite3 *pTo;
sqlite3 *pFrom;

/* Open the database file identified by backupDbName. Exit early if this fails
** for any reason. */
rc = sqlite3_open(backupDbName, &pFile);
if( rc==SQLITE_OK ){

pFrom = (isSave ? currentDb : pFile);
pTo = (isSave ? pFile : currentDb);

pBackup = sqlite3_backup_init(pTo, “main”, pFrom, “main”);
if( pBackup ){
(void)sqlite3_backup_step(pBackup, -1);
(void)sqlite3_backup_finish(pBackup);
}

rc = sqlite3_errcode(pTo);
}

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s