This document covers SQLite's advanced APIs including backup operations, blob I/O, sessions, and threading features. This is a machine-generated summary of documentation found on sqlite.org used as a reference during development.
The backup API allows copying database content between two databases while they are in use.
sqlite3_backup *sqlite3_backup_init(
sqlite3 *pDest, /* Destination database handle */
const char *zDestName, /* Destination database name */
sqlite3 *pSource, /* Source database handle */
const char *zSourceName /* Source database name */
);
int sqlite3_backup_step(sqlite3_backup *p, int nPage);
int sqlite3_backup_finish(sqlite3_backup *p);
int sqlite3_backup_remaining(sqlite3_backup *p);
int sqlite3_backup_pagecount(sqlite3_backup *p);
Usage:
sqlite3_backup_init()
to create backup objectsqlite3_backup_step()
repeatedly to copy pagessqlite3_backup_finish()
to clean upParameters:
nPage
: Number of pages to copy (-1 for all)Example:
sqlite3_backup *pBackup = sqlite3_backup_init(pDestDb, "main", pSrcDb, "main");
if (pBackup) {
sqlite3_backup_step(pBackup, -1); // Copy entire database
sqlite3_backup_finish(pBackup);
}
Reference: https://sqlite.org/c3ref/backup_finish.html
Blob I/O provides incremental read/write access to blob values without loading entire blobs into memory.
int sqlite3_blob_open(
sqlite3*,
const char *zDb,
const char *zTable,
const char *zColumn,
sqlite3_int64 iRow,
int flags,
sqlite3_blob **ppBlob
);
Parameters:
zDb
: Database name ("main", "temp", or attached name)zTable
: Table namezColumn
: Column nameiRow
: Row IDflags
: 0 for read-only, 1 for read-writeint sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
int sqlite3_blob_close(sqlite3_blob *);
int sqlite3_blob_bytes(sqlite3_blob *);
int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
Example:
sqlite3_blob *pBlob;
int rc = sqlite3_blob_open(db, "main", "mytable", "data", rowid, 0, &pBlob);
if (rc == SQLITE_OK) {
int size = sqlite3_blob_bytes(pBlob);
char *buffer = malloc(size);
sqlite3_blob_read(pBlob, buffer, size, 0);
sqlite3_blob_close(pBlob);
free(buffer);
}
Reference: https://sqlite.org/c3ref/blob_open.html
The session extension provides change tracking and conflict resolution capabilities.
int sqlite3session_create(
sqlite3 *db, /* Database handle */
const char *zDb, /* Database name */
sqlite3_session **ppSession /* OUT: New session object */
);
void sqlite3session_delete(sqlite3_session *pSession);
int sqlite3session_attach(
sqlite3_session *pSession, /* Session object */
const char *zTab /* Table name or NULL for all */
);
int sqlite3session_changeset(
sqlite3_session *pSession, /* Session object */
int *pnChangeset, /* OUT: Size of changeset */
void **ppChangeset /* OUT: Changeset blob */
);
int sqlite3session_patchset(
sqlite3_session *pSession, /* Session object */
int *pnPatchset, /* OUT: Size of patchset */
void **ppPatchset /* OUT: Patchset blob */
);
int sqlite3changeset_apply(
sqlite3 *db, /* Apply changes to this db */
int nChangeset, /* Size of changeset */
void *pChangeset, /* Changeset blob */
int(*xFilter)(void *pCtx, const char *zTab),
int(*xConflict)(void *pCtx, int eConflict, sqlite3_changeset_iter *p),
void *pCtx /* First argument to callbacks */
);
Reference: https://sqlite.org/sessionintro.html
int sqlite3_threadsafe(void);
Returns:
sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
void sqlite3_mutex_enter(sqlite3_mutex*);
int sqlite3_mutex_try(sqlite3_mutex*);
void sqlite3_mutex_leave(sqlite3_mutex*);
int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
int sqlite3_busy_timeout(sqlite3*, int ms);
Description: Set handler for locked database situations.
Example:
// Simple timeout
sqlite3_busy_timeout(db, 5000); // 5 second timeout
// Custom handler
int busyHandler(void *pArg, int count) {
if (count < 10) {
usleep(100000); // Sleep 100ms
return 1; // Try again
}
return 0; // Give up
}
sqlite3_busy_handler(db, busyHandler, NULL);
Reference: https://sqlite.org/c3ref/busy_handler.html
int sqlite3_unlock_notify(
sqlite3 *pBlocked,
void (*xNotify)(void **apArg, int nArg),
void *pNotifyArg
);
Description: Request notification when database is unlocked.
Reference: https://sqlite.org/unlock_notify.html
void *sqlite3_update_hook(
sqlite3*,
void(*)(void *,int ,char const *,char const *,sqlite3_int64),
void*
);
Callback parameters:
void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
void *sqlite3_wal_hook(
sqlite3*,
int(*)(void *,sqlite3*,const char*,int),
void*
);
Callback parameters:
int sqlite3_set_authorizer(
sqlite3*,
int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
void *pUserData
);
Action codes include:
SQLITE_CREATE_TABLE
SQLITE_INSERT
SQLITE_UPDATE
SQLITE_DELETE
SQLITE_SELECT
SQLITE_PRAGMA
Return values:
SQLITE_OK
- AllowSQLITE_DENY
- Deny with errorSQLITE_IGNORE
- Silently ignoreReference: https://sqlite.org/c3ref/set_authorizer.html
void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
Parameters:
int sqlite3_load_extension(
sqlite3 *db,
const char *zFile,
const char *zProc,
char **pzErrMsg
);
int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
Parameters:
zFile
: Extension filenamezProc
: Entry point (NULL for default)pzErrMsg
: Error message pointerint sqlite3_auto_extension(void(*xEntryPoint)(void));
int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
void sqlite3_reset_auto_extension(void);
Reference: https://sqlite.org/loadext.html
int sqlite3_wal_checkpoint_v2(
sqlite3 *db,
const char *zDb,
int eMode,
int *pnLog,
int *pnCkpt
);
Checkpoint modes:
SQLITE_CHECKPOINT_PASSIVE
- Checkpoint as much as possibleSQLITE_CHECKPOINT_FULL
- Wait for writers, checkpoint allSQLITE_CHECKPOINT_RESTART
- Like FULL, also wait for readersSQLITE_CHECKPOINT_TRUNCATE
- Like RESTART, also truncate WALconst char *sqlite3_filename_database(const char*);
const char *sqlite3_filename_journal(const char*);
const char *sqlite3_filename_wal(const char*);
int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
Status operations:
SQLITE_DBSTATUS_LOOKASIDE_USED
- Lookaside memory usedSQLITE_DBSTATUS_CACHE_USED
- Page cache memory usedSQLITE_DBSTATUS_SCHEMA_USED
- Schema memory usedSQLITE_DBSTATUS_STMT_USED
- Statement memory usedSQLITE_DBSTATUS_LOOKASIDE_HIT
- Lookaside hitsSQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
- Lookaside miss due to sizeSQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
- Lookaside miss due to fullSQLITE_DBSTATUS_CACHE_HIT
- Page cache hitsSQLITE_DBSTATUS_CACHE_MISS
- Page cache missesSQLITE_DBSTATUS_CACHE_WRITE
- Page cache writesSQLITE_DBSTATUS_DEFERRED_FKS
- Deferred foreign key constraintsSQLITE_DBSTATUS_CACHE_USED_SHARED
- Shared cache memorySQLITE_DBSTATUS_CACHE_SPILL
- Cache spillsReference: https://sqlite.org/c3ref/db_status.html