SQLite Forum

Query about backup API
Login

Query about backup API

(1) By anonymous on 2022-01-17 00:08:18 [link] [source]

Hello,

I am working to take backup up of DB content from "in memory" DB to "Actual disk". Database contains multiple tables. After backup is completed, "in memory" database content will be erased.

Each time backup is taken, I would like to retain existing content and add new content at the end of each table.

I am using online backup API as specified in https://www.sqlite.org/backup.html. Each time database content is copied from "in memory" DB to "file" on disk, existing database content is replaced.

Is there a way to original content and append new content?

Code snippet:

sqlite3 *pFile;

constexpr uint8_t flags {SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE };

// open database connection

if (sqlite3_open_v2(dbPath.c_str(), &pFile, flags, nullptr) == SQLITE_OK) {

static_cast<void>(sqlite3_exec(pFile, "PRAGMA journal_mode = WAL", nullptr , nullptr, nullptr));

static_cast<void>(sqlite3_exec(pFile, "PRAGMA synchronous = NORMAL", nullptr , nullptr, nullptr));

static_cast<void>(sqlite3_exec(pFile, "PRAGMA auto_vacuum = 1", nullptr , nullptr, nullptr));

sqlite3_backup *pBackup;

sqlite3 *pTo;

sqlite3 *pFrom;

pFrom = handle;

pTo = pFile;

const string mainDB {"main"};

pBackup = sqlite3_backup_init(pTo, mainDB.c_str(), pFrom, mainDB.c_str());

if( nullptr != pBackup ){

    static_cast<void>(sqlite3_backup_step(pBackup, -1));

    static_cast<void>(sqlite3_backup_finish(pBackup));

}

const int32_t rc {sqlite3_errcode(pTo)};

if (rc != SQLITE_OK)

{

  //Log Print

}

// Close database memory connection

static_cast<void>(sqlite3_close_v2(pFile));

Thank you.

(2) By Simon Slavin (slavin) on 2022-01-17 09:55:22 in reply to 1 [source]

The backup API backs up the entire database. Not rows of data.

If you just want to add the rows of one table to the existing rows of another table you could attach the destination database to your in-memory connection, and then use

INSERT INTO destination.myTable (SELECT * from myTable)

(3) By anonymous on 2022-01-17 18:54:55 in reply to 2 [link] [source]

Thank you for the response.

How will this change impact performance? Any idea?

(4) By Simon Slavin (slavin) on 2022-01-17 22:51:59 in reply to 3 [link] [source]

Sorry, I couldn't possibly guess which is faster, or how that might change depending on whether your two databases are on different media. Too much depends on your hardware setup.

Also, it's difficult to assess the performance of the backup API because every time another process makes a change to the database it has to restart.

(5) By anonymous on 2022-01-18 15:32:58 in reply to 4 [link] [source]

I Understand. Thanks again for the response.