sqlite3.h

Overview

The sqlite3.h header file defines the public C-language API for the SQLite library. It provides the interfaces and constants that client programs use to interact with SQLite database connections, execute SQL statements, manage prepared statements, handle errors, configure the SQLite environment, and more. This file is the authoritative source for all stable and experimental API functions, structures, types, and constants that SQLite exposes.

Main Components

1. Database Connection and Statement Handles

2. Result Codes and Extended Result Codes

3. File and VFS Interfaces

4. Memory Allocation and Mutex Interfaces

5. SQL Execution and Prepared Statements

6. Error Handling

7. Configuration Interfaces

8. Utility Functions

9. SQL Function and Aggregate Creation

10. Authorizer and Busy Handler

11. Trace and Profile Callbacks

12. Progress Handlers and Interrupts


Detailed Explanations

sqlite3_open(), sqlite3_open16(), sqlite3_open_v2()

Purpose:
Open a new SQLite database connection.

Parameters:

Return:
SQLITE_OK on success or an error code otherwise.

Usage Example:

sqlite3 *db;
int rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
    // handle error
}

sqlite3_close(), sqlite3_close_v2()

Purpose:
Close an open SQLite database connection and release resources.

Parameters:

Return:
SQLITE_OK on success. sqlite3_close() returns SQLITE_BUSY if unfinalized statements or unfinished objects exist, while sqlite3_close_v2() marks the connection as zombie and returns SQLITE_OK.

Usage Notes:
Finalize all prepared statements and close all BLOB handles before closing the database to avoid SQLITE_BUSY.

sqlite3_exec()

Purpose:
Execute one or more SQL statements in a single call with optional callback for each result row.

Parameters:

Return:
SQLITE_OK on success or an error code.

Usage Example:

int callback(void *data, int argc, char **argv, char **azColName){
    for(int i = 0; i < argc; i++){
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return 0;
}

char *errmsg = 0;
sqlite3_exec(db, "SELECT * FROM table;", callback, 0, &errmsg);
if (errmsg) {
    // handle error
    sqlite3_free(errmsg);
}

sqlite3_prepare_v2()

Purpose:
Compile SQL text into a prepared statement.

Parameters:

Return:
SQLITE_OK on success or an error code.

Usage:
A prepared statement can be executed via sqlite3_step() repeatedly, reset with sqlite3_reset(), and finalized with sqlite3_finalize().

sqlite3_step()

Purpose:
Execute a prepared statement or advance it to the next result row.

Parameters:

Return:

Usage:

while(sqlite3_step(stmt) == SQLITE_ROW){
    // process row
}

sqlite3_column_*() Functions

Retrieve individual column data from the current row of a result set.

sqlite3_bind_*() Functions

Bind parameters to SQL statements before execution.

Parameter Indexing:
Parameters are 1-indexed. Named parameters can be queried with sqlite3_bind_parameter_index().

sqlite3_finalize()

Purpose:
Delete a prepared statement and release associated resources.

Parameters:

Return:
SQLITE_OK on successful finalization, or error code if last run failed.

sqlite3_reset()

Purpose:
Reset a prepared statement to its initial state for re-execution.

Parameters:

Return:
SQLITE_OK if reset successful.

sqlite3_create_function(), sqlite3_create_function_v2()

Purpose:
Register or redefine SQL scalar, aggregate, or window functions.

Parameters:

sqlite3_set_authorizer()

Purpose:
Set a callback to authorize or deny certain SQL operations during statement compilation.

Parameters:

Authorizer Return Codes:

sqlite3_busy_handler() and sqlite3_busy_timeout()

Purpose:
Set handlers to respond when database is locked (SQLITE_BUSY).

sqlite3_trace_v2()

Purpose:
Register a callback for tracing SQL statement execution and profiling.

Parameters:

sqlite3_progress_handler()

Purpose:
Invoke a callback periodically during long-running SQL operations to enable UI updates or cancellation.

Parameters:

sqlite3_interrupt()

Purpose:
Request immediate abort of any currently running SQL operation on a database connection.

Parameters:


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Flowchart of Key API Interactions

flowchart TD
A["sqlite3_open()"] --> B[sqlite3 database connection]
B --> C["sqlite3_prepare_v2()"]
C --> D[sqlite3_stmt prepared statement]
D --> E["sqlite3_bind_*() parameter binding"]
D --> F["sqlite3_step() execute statement"]
F -- if SQLITE_ROW --> G["Access column data (sqlite3_column_*)"]
F -- if SQLITE_DONE --> H["sqlite3_reset() or sqlite3_finalize()"]
B --> I["sqlite3_exec() convenience interface"]
B --> J["sqlite3_close()"]
B --> K["sqlite3_set_authorizer()"]
B --> L["sqlite3_busy_handler() / sqlite3_busy_timeout()"]
B --> M["sqlite3_trace_v2()"]
B --> N["sqlite3_progress_handler()"]
B --> O["sqlite3_interrupt()"]

This flowchart shows how a typical SQLite usage scenario flows from opening a connection, preparing statements, binding parameters, executing queries, accessing results, and finally cleaning up, as well as how auxiliary features like authorizers, busy handlers, tracing, progress callbacks, and interrupts integrate with the connection.


Selected Constants and Macros


Usage Notes


This file is fundamental to the SQLite C API and interacts with many other components such as virtual tables, custom VFS implementations, and user-defined SQL functions. It covers the core functionality for database connection management, SQL statement preparation and execution, error handling, configuration, and extension mechanisms.

For further details, see related topics such as Prepared Statements, Virtual File System (VFS), Error Handling in SQLite, and User-Defined Functions.