sqlite3.h

Overview

sqlite3.h is the primary C language header file that defines the public API for the SQLite library. It specifies the interfaces, constants, data types, and function prototypes that client applications use to interact with SQLite databases. This file embodies the contract between SQLite and its clients, detailing how to open, configure, execute queries against, and manage SQLite database connections and statements.

The file includes definitions for key SQLite abstractions such as database connections (sqlite3), prepared statements (sqlite3_stmt), virtual file system interfaces (sqlite3_vfs), and file I/O methods (sqlite3_io_methods). It also provides mechanisms for configuration, error handling, memory management, SQL compilation, execution, and extension registration.

This header is used by applications linking against the SQLite library or its variants (e.g., SQLCipher) to embed SQL database functionality.


Key Data Types and Structures

sqlite3

An opaque structure representing a database connection handle. It acts as the core object for all database operations.

sqlite3_stmt

An opaque structure representing a compiled SQL statement (prepared statement).

sqlite3_file

Represents an open file in the VFS (Virtual File System) layer.

sqlite3_io_methods

Defines virtual methods for file I/O operations used by the SQLite core.

sqlite3_vfs

Represents an OS interface layer or Virtual File System.

sqlite3_value and sqlite3_context

sqlite3_mem_methods

Defines an interface for custom memory allocators to replace SQLite’s default allocator.


Important Constants and Macros


Primary Functions and Their Usage

Initialization and Shutdown

Opening and Closing Database Connections

SQL Execution

Binding Parameters to Prepared Statements

Retrieving Result Column Data

Error Handling

Configuration and Diagnostics

Memory Management

Virtual File System (VFS) Interaction

SQL Function and Aggregate Registration


Important Implementation Details and Algorithms


Interaction with Other System Components


Selected Function Documentation Examples

int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs)


int sqlite3_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail)


int sqlite3_step(sqlite3_stmt *pStmt)


int sqlite3_bind_int(sqlite3_stmt*, int, int)


const void *sqlite3_column_text(sqlite3_stmt*, int iCol)


Visual Diagram

flowchart TD
subgraph Database Connection (sqlite3)
A[Open: sqlite3_open*] --> B[Prepare Statement: sqlite3_prepare*]
B --> C[Bind Parameters: sqlite3_bind_*]
C --> D[Execute: sqlite3_step]
D --> E{Result?}
E -->|Row Data| F[Access Columns: sqlite3_column_*]
E -->|Done| G[Reset: sqlite3_reset]
G --> C
G -->|Or| H[Finalize: sqlite3_finalize]
H --> I[Close Connection: sqlite3_close*]
end
subgraph VFS Layer (sqlite3_vfs)
J[VFS xOpen] --> K[sqlite3_file with sqlite3_io_methods]
K --> L[File I/O: xRead, xWrite, xLock, xSync, ...]
end
A ---|Uses| J

Notes on Usage and Interaction


Reference to Related Topics