sqlite3.c
Overview
The sqlite3.c file is the amalgamated source code for the core SQLite library version 3.50.4. It combines many individual C source files into a single translation unit to optimize compiler performance and enable advanced optimizations. This file includes the entire SQLite core implementation, providing all necessary functionality to compile and link SQLite into an application. It excludes the command-line shell and language-specific wrappers, which are implemented separately.
This file contains the internal and public APIs, core data structures, constants, and utility functions that make up the SQLite engine, including database connection management, SQL compilation and execution, virtual file system (VFS) interfaces, concurrency control, and more.
Detailed Descriptions of Main Components
Core Data Structures and Types
sqlite3: Opaque structure representing a database connection. Handles all operations and states related to an open SQLite database instance.
sqlite3_stmt: Opaque structure representing a compiled SQL statement (prepared statement). It encapsulates the bytecode and execution state of a single SQL statement.
sqlite3_value: Represents a dynamically typed SQL value. Values can be integers, floating-point numbers, strings, BLOBs, or NULLs.
sqlite3_context: Represents the context in which an SQL function executes. Used in user-defined functions for returning results, accessing auxiliary data, etc.
sqlite3_file and sqlite3_io_methods: Abstractions for VFS file handles and their associated I/O methods. These are used to implement platform-independent file operations.
sqlite3_vfs: Defines the interface between SQLite and the underlying operating system or platform. This includes methods for file handling, randomness, time, dynamic loading, and more.
sqlite3_mem_methods: Defines memory allocation routines used internally by SQLite. Allows applications to override default memory management.
sqlite3_mutex: Abstract mutex object used for thread safety and concurrency control.
Important Constants and Flags
Result Codes: Defines standard and extended result codes such as
SQLITE_OK, SQLITE_ERROR, SQLITE_BUSY,SQLITE_ROW,SQLITE_DONE, and many detailed extended error codes for precise error handling.File Open Flags: Flags used for opening database files via VFS, such as SQLITE_OPEN_READONLY, SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, and others controlling journal modes, shared cache, mutex behavior, and more.
File Locking Levels: Lock states like SQLITE_LOCK_NONE, SQLITE_LOCK_SHARED, SQLITE_LOCK_RESERVED, etc., used to manage concurrency and file access.
Synchronization Flags: Control how file synchronization operations behave, e.g., SQLITE_SYNC_NORMAL, SQLITE_SYNC_FULL, SQLITE_SYNC_DATAONLY.
Text Encodings: Codes for supported text encodings such as SQLITE_UTF8, SQLITE_UTF16LE, SQLITE_UTF16BE, SQLITE_UTF16.
Authorizer Action Codes: Codes used in authorization callbacks to control SQL statement permissions, e.g., SQLITE_CREATE_TABLE, SQLITE_INSERT, SQLITE_SELECT, etc.
Key Functions and Methods
Initialization and Configuration
int sqlite3_initialize(void);Initializes the SQLite library, setting up internal data structures, default VFS, and configurations. Must be called before any other SQLite operation unless auto-initialization is enabled.
int sqlite3_shutdown(void);Deinitializes the SQLite library, freeing resources. Should be called after closing all database connections.
int sqlite3_config(int, ...);Allows global configuration of SQLite before initialization (or after shutdown) such as threading mode, memory allocators, mutex handlers, logging, and URI handling.
int sqlite3_db_config(sqlite3*, int op, ...);Allows configuration of individual database connections, for example lookaside memory settings, foreign key enforcement, trigger enabling, etc.
Database Connection Management
int sqlite3_open(const char *filename, sqlite3 **ppDb);Opens a new database connection to a file named by
filename. ReturnsSQLITE_OKon success and fills*ppDbwith a database handle.int sqlite3_open16(const void *filename, sqlite3 **ppDb);Similar to sqlite3_open() but accepts a UTF-16 encoded filename.
int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs);Extended open function allowing flags for read/write mode, create, URI interpretation, mutex behavior, and specifying a custom VFS.
int sqlite3_close(sqlite3*);Closes a database connection, releasing all associated resources. Returns
SQLITE_OKon success.int sqlite3_close_v2(sqlite3*);Similar to sqlite3_close() but can be used safely with garbage-collected languages by deferring resource deallocation until safe.
SQL Statement Preparation and Execution
int sqlite3_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail);Compiles the first SQL statement in
zSqlinto a prepared statement object.nBytespecifies the length of the SQL string, or -1 if null-terminated. On success,*ppStmtpoints to the compiled statement.int sqlite3_step(sqlite3_stmt*);Executes a single step of a prepared statement, returning
SQLITE_ROWif a row of data is ready,SQLITE_DONEif execution is complete, or an error code.int sqlite3_finalize(sqlite3_stmt *pStmt);Deletes a prepared statement object and frees associated resources.
int sqlite3_reset(sqlite3_stmt *pStmt);Resets a prepared statement to its initial state, ready to be re-executed. Does not clear bound parameters.
Parameter Binding
int sqlite3_bind_int(sqlite3_stmt*, int, int);Binds an integer value to the N-th SQL parameter in a prepared statement.
int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int, void()(void));Binds a UTF-8 text string to the N-th SQL parameter.
int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void()(void));Binds a BLOB value to the N-th SQL parameter.
int sqlite3_bind_null(sqlite3_stmt*, int);Binds a NULL value to the N-th SQL parameter.
Additional binding functions exist for 64-bit integers, UTF-16 text, zeroblobs, and pointers.
Result Retrieval
int sqlite3_column_count(sqlite3_stmt *pStmt);Returns the number of columns in the result set of a prepared statement.
const char sqlite3_column_name(sqlite3_stmt, int N);Returns the name of the N-th column in the result set.
const void sqlite3_column_blob(sqlite3_stmt, int iCol);Returns BLOB data from the specified column of the current row.
const unsigned char sqlite3_column_text(sqlite3_stmt, int iCol);Returns UTF-8 text from the specified column.
int sqlite3_column_type(sqlite3_stmt*, int iCol);Returns the datatype code of the specified column.
Other interfaces provide access to integers, doubles, 64-bit integers, UTF-16 text, and the raw
sqlite3_valueobject.
Utility and Miscellaneous Functions
int sqlite3_exec(sqlite3*, const char sql, int(callback)(void,int,char,char*), void *, char **errmsg);Convenience wrapper to execute multiple SQL statements without explicit preparation or stepping. Invokes callback for each row.
int sqlite3_busy_handler(sqlite3*, int()(void,int), void*);Sets a callback invoked when a table is locked, allowing applications to retry or abort.
int sqlite3_busy_timeout(sqlite3*, int ms);Sets a busy handler that waits up to
msmilliseconds before returning SQLITE_BUSY.void sqlite3_interrupt(sqlite3*);Interrupts any pending operation on the database connection.
const char sqlite3_errmsg(sqlite3);Returns English-language description of the most recent error.
int sqlite3_threadsafe(void);Returns whether the SQLite library was compiled with thread safety enabled.
Various functions for memory allocation, logging, random number generation, and URI parameter handling.
Authorization and Security
int sqlite3_set_authorizer(sqlite3*, int (xAuth)(void,int,const char*,const char*,const char*,const char*), void *pUserData);Registers an authorizer callback that is invoked during SQL compilation to permit or deny certain operations.
Authorizer action codes define the various types of SQL actions subject to authorization.
Implementation Details and Algorithms
The amalgamation technique used here combines all source files into one, enabling the compiler to perform more global optimizations such as better function inlining and constant folding.
SQLite uses an internal virtual machine to execute bytecode generated by the SQL compiler. Prepared statements represent these bytecode programs.
The VFS layer abstracts filesystem operations, allowing SQLite to port across multiple platforms and filesystems.
Mutexes and file locking mechanisms are implemented to ensure thread safety and concurrency control based on compile-time and runtime configuration.
Extended result codes provide fine-grained error reporting, improving debugging and error handling.
The API supports dynamic typing of SQL values, parameter binding with multiple encoding options, and incremental step-wise query execution.
Memory management can be customized through the
sqlite3_mem_methodsinterface.
Interaction with Other Parts of the System
VFS Layer:
sqlite3_vfsand related structures define the interface to the underlying OS or platform, handling file operations, randomness, time, and dynamic loading.Application Layer: Applications interact with SQLite via opaque pointers (
sqlite3,sqlite3_stmt), using the public API to open connections, prepare statements, bind parameters, execute queries, and fetch results.Extensions and Virtual Tables: The function creation interfaces allow application-defined SQL functions and aggregates, enabling extensibility.
Error Handling: Error codes and messages propagate through the API, allowing applications to react appropriately.
Threading and Concurrency: Mutexes and busy handlers support thread safety and concurrency control.
URI Handling: Filename URI parameters allow customization of the behavior of SQLite connections.
Usage Examples
sqlite3 *db;
sqlite3_stmt *stmt;
const char *sql = "SELECT name, age FROM users WHERE id = ?;";
int rc;
// Open database
rc = sqlite3_open("mydb.sqlite", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
return rc;
}
// Prepare statement
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return rc;
}
// Bind parameter
sqlite3_bind_int(stmt, 1, 42);
// Execute and fetch results
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const unsigned char *name = sqlite3_column_text(stmt, 0);
int age = sqlite3_column_int(stmt, 1);
printf("Name: %s, Age: %d\n", name, age);
}
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution error: %s\n", sqlite3_errmsg(db));
}
// Finalize and close
sqlite3_finalize(stmt);
sqlite3_close(db);
Visual Diagram of Core File Structure and Relationships
flowchart TD
A[sqlite3.c File] --> B[sqlite3 - Database Connection]
A --> C[sqlite3_stmt - Prepared Statement]
A --> D[sqlite3_value - SQL Value]
A --> E[sqlite3_context - Function Context]
A --> F[sqlite3_file & sqlite3_io_methods - VFS File Handling]
A --> G[sqlite3_vfs - Virtual File System Interface]
A --> H[sqlite3_mem_methods - Memory Allocation]
A --> I[sqlite3_mutex - Mutex Abstraction]
B --> C
C --> D
C --> E
G --> F
H --> B
I --> B
subgraph API Functions
B_open["sqlite3_open*()"]
B_close["sqlite3_close*()"]
C_prepare["sqlite3_prepare*()"]
C_step["sqlite3_step()"]
C_finalize["sqlite3_finalize()"]
C_bind["sqlite3_bind*()"]
C_column["sqlite3_column*()"]
B_exec["sqlite3_exec()"]
B_config["sqlite3_config()"]
B_set_authorizer["sqlite3_set_authorizer()"]
end
B --> B_open
B --> B_close
B --> B_exec
B --> B_config
B --> B_set_authorizer
C --> C_prepare
C --> C_step
C --> C_finalize
C --> C_bind
C --> C_column
This diagram illustrates the main data structures and their relationships along with the core API functions exposed in sqlite3.c.
References to Relevant Topics
For detailed descriptions of the SQLite threading modes and mutex configurations, see Threading Mode.
For more information about the VFS abstraction and how it interfaces with the operating system, see Virtual File System (VFS) Interface.
For an in-depth explanation of prepared statements and bytecode execution, see Prepared Statements and Virtual Machine.
To understand the authorization callback mechanism and action codes, refer to Authorization Callback.
For details on memory allocation customization and performance tuning, see Memory Allocation Subsystem.
To learn about URI filename handling and query parameters, see URI Filenames.
For information about error codes and extended result codes, consult SQLite Result Codes.
For SQL function creation and user-defined functions, see User-Defined SQL Functions.