Important: This documentation describes the underlying SQLite C library API, not the JavaScript API exposed by
@photostructure/sqlite. These C functions are not directly callable from JavaScript. For the JavaScript API, see API Reference.
This document covers the fundamental SQLite C/C++ APIs for database connections, basic operations, and error handling. This is a machine-generated summary of documentation found on sqlite.org, used as a reference during development.
- Core concepts
- Database connection
- SQL statement execution
- Error handling
- Transaction control
- Utility functions
SQLite uses several key objects:
- sqlite3: A database connection handle
- sqlite3_stmt: A prepared statement object
- sqlite3_value: A protected value object
- sqlite3_context: Function context for user-defined functions
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
int sqlite3_open16(
const void *filename, /* Database filename (UTF-16) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
int sqlite3_open_v2(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: SQLite db handle */
int flags, /* Flags */
const char *zVfs /* Name of VFS module to use */
);Opens a connection to an SQLite database file.
Special filenames:
:memory:- Creates a private, temporary in-memory database""(empty string) - Creates a private, temporary on-disk database
Flags for sqlite3_open_v2:
SQLITE_OPEN_READONLY- Open in read-only modeSQLITE_OPEN_READWRITE- Open for reading and writingSQLITE_OPEN_CREATE- Create if doesn't existSQLITE_OPEN_URI- Interpret filename as URISQLITE_OPEN_MEMORY- Use in-memory databaseSQLITE_OPEN_NOMUTEX- No mutex (multi-thread unsafe)SQLITE_OPEN_FULLMUTEX- Full mutex (serialized)SQLITE_OPEN_SHAREDCACHE- Enable shared cacheSQLITE_OPEN_PRIVATECACHE- Disable shared cache
Returns: SQLITE_OK on success, error code otherwise
Reference: https://sqlite.org/c3ref/open.html
int sqlite3_close(sqlite3*);
int sqlite3_close_v2(sqlite3*);Closes a database connection and releases resources.
Difference:
sqlite3_close()- ReturnsSQLITE_BUSYif resources not freedsqlite3_close_v2()- Marks connection as zombie, closes when possible
Reference: https://sqlite.org/c3ref/close.html
int sqlite3_exec(
sqlite3*, /* Database handle */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);Convenience wrapper that executes SQL without preparing statements.
Callback parameters:
- First: User data pointer
- Second: Number of columns
- Third: Array of column values (as strings)
- Fourth: Array of column names
Reference: https://sqlite.org/c3ref/exec.html
int sqlite3_errcode(sqlite3 *db);
int sqlite3_extended_errcode(sqlite3 *db);
const char *sqlite3_errmsg(sqlite3*);
const void *sqlite3_errmsg16(sqlite3*);
const char *sqlite3_errstr(int);Retrieves error information.
Reference: https://sqlite.org/c3ref/errcode.html
Primary result codes:
SQLITE_OK(0) - SuccessSQLITE_ERROR(1) - Generic errorSQLITE_INTERNAL(2) - Internal logic errorSQLITE_PERM(3) - Access permission deniedSQLITE_ABORT(4) - Callback requested abortSQLITE_BUSY(5) - Database file is lockedSQLITE_LOCKED(6) - Table is lockedSQLITE_NOMEM(7) - Memory allocation failedSQLITE_READONLY(8) - Attempt to write readonly databaseSQLITE_INTERRUPT(9) - Interrupted by sqlite3_interrupt()SQLITE_IOERR(10) - Disk I/O errorSQLITE_CORRUPT(11) - Database disk image malformedSQLITE_FULL(13) - Database is fullSQLITE_CANTOPEN(14) - Unable to open database fileSQLITE_CONSTRAINT(19) - Constraint violationSQLITE_MISMATCH(20) - Data type mismatchSQLITE_MISUSE(21) - Library used incorrectlySQLITE_ROW(100) - Row readySQLITE_DONE(101) - No more rows
Reference: https://sqlite.org/rescode.html
SQLite transactions are controlled using SQL statements executed through the API:
BEGIN [DEFERRED|IMMEDIATE|EXCLUSIVE] [TRANSACTION]
COMMIT [TRANSACTION]
ROLLBACK [TRANSACTION]
SAVEPOINT savepoint_name
RELEASE [SAVEPOINT] savepoint_name
ROLLBACK TO [SAVEPOINT] savepoint_nameTo check if a database is in autocommit mode:
int sqlite3_get_autocommit(sqlite3*);To set up commit and rollback hooks:
void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);Reference: https://sqlite.org/lang_transaction.html
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);int sqlite3_changes(sqlite3*);
int sqlite3_total_changes(sqlite3*);void sqlite3_interrupt(sqlite3*);const char *sqlite3_libversion(void);
const char *sqlite3_sourceid(void);
int sqlite3_libversion_number(void);void sqlite3_randomness(int N, void *P);int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
int sqlite3_status64(int op, sqlite3_int64 *pCurrent, sqlite3_int64 *pHighwater, int resetFlag);#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
#define SQLITE_OPEN_SUPER_JOURNAL 0x00004000 /* VFS only */
#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
#define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_EXRESCODE 0x02000000 /* Extended result codes */- SQLite C/C++ Interface: https://sqlite.org/c3ref/intro.html
- SQLite Introduction: https://sqlite.org/cintro.html
- Result Codes: https://sqlite.org/rescode.html
- SQL Syntax: https://sqlite.org/lang.html