SQLite#
Sync SQLite adapter using Python's built-in sqlite3 module with thread-local
connection pooling.
Configuration#
- class sqlspec.adapters.sqlite.SqliteConfig[source]#
Bases:
SyncDatabaseConfig[Connection,SqliteConnectionPool,SqliteDriver]SQLite configuration with thread-local connections.
- driver_type#
alias of
SqliteDriver
- connection_type#
alias of
Connection
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, observability_config=None, **kwargs)[source]#
Initialize SQLite configuration.
- Parameters:
connection_config¶ (
SqliteConnectionParams|dict[str, typing.Any] |None) -- Configuration parameters including connection settingsconnection_instance¶ (
SqliteConnectionPool|None) -- Pre-created pool instancemigration_config¶ (
dict[str, typing.Any] |None) -- Migration configurationstatement_config¶ (
StatementConfig|None) -- Default SQL statement configurationdriver_features¶ (
SqliteDriverFeatures|dict[str, typing.Any] |None) -- Optional driver feature configurationbind_key¶ (
str|None) -- Optional bind key for the configurationextension_config¶ (
dict[str,dict[str,Any] |LitestarConfig|FastAPIConfig|StarletteConfig|SanicConfig|FlaskConfig|ADKConfig|EventsConfig|OpenTelemetryConfig|PrometheusConfig] |None) -- Extension-specific configurationobservability_config¶ (
ObservabilityConfig|None) -- Adapter-level observability overrides for lifecycle hooks and observers**kwargs¶ (
Any) -- Additional keyword arguments passed to the base configuration.
- create_connection()[source]#
Open a standalone connection owned by the caller.
The connection carries the same parameters, PRAGMAs, and runtime setup the pool applies, but it is not the pool's thread-local connection, so closing it leaves the pool usable.
- Returns:
A newly opened connection.
- Return type:
Connection Parameters#
Driver Features#
- class sqlspec.adapters.sqlite.SqliteDriverFeatures[source]#
Bases:
TypedDictSQLite driver feature configuration.
Controls optional type handling and serialization features for SQLite connections.
- enable_custom_adapters: Enable custom type adapters for JSON/UUID/datetime conversion.
Defaults to True for enhanced Python type support. Set to False only if you need pure SQLite behavior without type conversions.
- json_serializer: Custom JSON serializer function.
Defaults to sqlspec.utils.serializers.to_json.
- json_deserializer: Custom JSON deserializer function.
Defaults to sqlspec.utils.serializers.from_json.
- on_connection_create: Callback executed when a connection is created.
Receives the raw sqlite3 connection for low-level driver configuration. Runs after internal setup (PRAGMA optimizations).
- enable_events: Enable database event channel support.
Defaults to True when extension_config["events"] is configured. Provides pub/sub capabilities via table-backed queue (SQLite has no native pub/sub). Requires extension_config["events"] for migration setup.
- events_backend: Event channel backend selection.
Only option: "poll_queue" (durable table-backed queue with lease-based retries and acknowledgements). SQLite does not have native pub/sub, so poll_queue is the only backend. Defaults to "poll_queue".
- custom_functions: Register SQL functions that run on the connection thread.
Each entry must include name, narg, and func.
- custom_collations: Register SQL collations that compare two string values.
Each entry must include name and func.
- custom_aggregates: Register SQL aggregates with step/finalize classes.
Each entry must include name, narg, and aggregate_class.
- custom_window_functions: Register user-defined aggregate window functions.
Each entry must include name, narg, and window_class.
default_transaction_mode: Default SQLite transaction mode (DEFERRED, IMMEDIATE, or EXCLUSIVE). authorizer_callback: sqlite3 authorizer hook run during statement compilation. trace_callback: sqlite3 trace hook run for executed statements. progress_handler: sqlite3 progress hook run every progress_handler_interval VM opcodes. progress_handler_interval: Progress callback interval in SQLite virtual machine opcodes.
Must be a positive integer when provided.
- row_factory: Row factory selector or callable used for raw sqlite3 connections.
"row" maps to sqlite3.Row, "dict" maps to a dict row adapter, "tuple" keeps tuple rows. "dict" and custom callables can change raw connection result shapes seen by callers.
text_factory: Text factory used for raw sqlite3 connections. pragmas: Additional PRAGMA settings applied after built-in optimization PRAGMAs.
User values override built-in defaults when the same PRAGMA appears in both places.
extensions: Shared-library extension paths loaded on each connection.
User-Defined Functions and Extensions#
- class sqlspec.adapters.sqlite.SqliteFunctionConfig[source]#
Bases:
TypedDictUser-defined SQLite function registration.
Driver#
- class sqlspec.adapters.sqlite.SqliteDriver[source]#
Bases:
SyncDriverAdapterBaseSQLite driver implementation.
Provides SQL statement execution, transaction management, and result handling for SQLite databases using the standard sqlite3 module.
- __init__(connection, statement_config=None, driver_features=None)[source]#
Initialize SQLite driver.
- Parameters:
connection¶ (
Connection) -- SQLite database connectionstatement_config¶ (
StatementConfig|None) -- Statement configuration settingsdriver_features¶ (
dict[str, typing.Any] |None) -- Driver-specific feature flags
- dispatch_execute(cursor, statement)[source]#
Execute single SQL statement.
- Parameters:
- Return type:
- Returns:
ExecutionResult with statement execution details
- dispatch_execute_many(cursor, statement)[source]#
Execute SQL with multiple parameter sets.
- Parameters:
- Return type:
- Returns:
ExecutionResult with batch execution details
- dispatch_execute_script(cursor, statement)[source]#
Execute SQL script with statement splitting and parameter handling.
- Parameters:
- Return type:
- Returns:
ExecutionResult with script execution details
- execute_many(statement, /, parameters, *filters, statement_config=None, **kwargs)[source]#
Execute many with a SQLite thin path for simple qmark batches.
- begin(mode=None)[source]#
Begin a database transaction.
- Parameters:
mode¶ (
Optional[Literal['DEFERRED','IMMEDIATE','EXCLUSIVE']]) -- Transaction lock mode (DEFERRED, IMMEDIATE, or EXCLUSIVE). Defaults to configured driver feature or SQLite default (DEFERRED).- Raises:
SQLSpecError -- If transaction cannot be started
- Return type:
- commit()[source]#
Commit the current transaction.
- Raises:
SQLSpecError -- If transaction cannot be committed
- Return type:
- rollback()[source]#
Rollback the current transaction.
- Raises:
SQLSpecError -- If transaction cannot be rolled back
- Return type:
- with_cursor(connection)[source]#
Create context manager for SQLite cursor.
- Parameters:
connection¶ (
Connection) -- SQLite database connection- Return type:
SqliteCursor- Returns:
Cursor context manager for safe cursor operations
- dispatch_select_stream(statement, chunk_size)[source]#
Return a native SQLite row stream backed by chunked
fetchmany.- Return type:
Optional[SyncRowStream[dict[str, typing.Any]]]
- handle_database_exceptions()[source]#
Handle database-specific exceptions and wrap them appropriately.
- Return type:
SqliteExceptionHandler- Returns:
Exception handler with deferred exception pattern for mypyc compatibility.
- select_to_storage(statement, destination, /, *parameters, statement_config=None, partitioner=None, format_hint=None, telemetry=None, **kwargs)[source]#
Execute a query and write Arrow-compatible output to storage (sync).
- Return type:
- load_from_arrow(table, source, *, batch_size=10000, partitioner=None, overwrite=False, telemetry=None)[source]#
Load Arrow data into SQLite using chunked batched inserts.
- Return type:
- load_from_storage(table, source, *, file_format, partitioner=None, overwrite=False)[source]#
Load staged artifacts from storage into SQLite.
- Return type:
- property data_dictionary: SqliteDataDictionary#
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
Connection Pool#
- class sqlspec.adapters.sqlite.pool.SqliteConnectionPool[source]#
Bases:
objectThread-local connection manager for SQLite.
SQLite connections aren't thread-safe, so we use thread-local storage to ensure each thread has its own connection. This is simpler and more efficient than a traditional pool for SQLite's constraints.
- __init__(connection_parameters, enable_optimizations=True, enable_foreign_keys=False, recycle_seconds=86400, health_check_interval=30.0, on_connection_create=None, runtime_setup=None)[source]#
Initialize the thread-local connection manager.
- Parameters:
connection_parameters¶ (
dict[str, typing.Any]) -- SQLite connection parametersenable_optimizations¶ (
bool) -- Whether to apply performance PRAGMAsenable_foreign_keys¶ (
bool) -- Whether to enable foreign-key enforcementrecycle_seconds¶ (
int) -- Connection recycle time in seconds (default 24h)health_check_interval¶ (
float) -- Seconds of idle time before running health checkon_connection_create¶ (typing.Callable[[<class 'sqlite3.Connection'>],
None] |None) -- Callback executed when connection is createdruntime_setup¶ (
dict[str, typing.Any] |None) -- Runtime feature configuration applied after internal PRAGMAs
- new_connection()[source]#
Create a standalone connection configured like a pooled one.
The result is owned by the caller: it is not thread-local and is not tracked for pool shutdown.
- Returns:
A newly opened, fully configured connection.
- Return type:
- get_connection()[source]#
Get a thread-local connection.
- Yields:
SqliteConnection -- A thread-local connection.
- acquire()[source]#
Acquire a thread-local connection.
- Returns:
A thread-local connection
- Return type:
- release(connection)[source]#
Release a connection (no-op for thread-local connections).
- Parameters:
connection¶ (
Connection) -- The connection to release (ignored)- Return type:
Data Dictionary#
- class sqlspec.adapters.sqlite.data_dictionary.SqliteDataDictionary[source]#
Bases:
SyncDataDictionaryBaseSQLite-specific sync data dictionary.
- dialect: ClassVar[str] = 'sqlite'#
Dialect identifier. Must be defined by subclasses as a class attribute.
- get_version(driver)[source]#
Get SQLite database version information.
- Parameters:
driver¶ (
SqliteDriver) -- Sync database driver instance.- Return type:
- Returns:
SQLite version information or None if detection fails.
- get_feature_flag(driver, feature)[source]#
Check if SQLite database supports a specific feature.
- Parameters:
driver¶ (
SqliteDriver) -- Sync database driver instance.
- Return type:
- Returns:
True if feature is supported, False otherwise.
- get_optimal_type(driver, type_category)[source]#
Get optimal SQLite type for a category.
- Parameters:
driver¶ (
SqliteDriver) -- Sync database driver instance.
- Return type:
- Returns:
SQLite-specific type name.
- get_tables(driver, schema=None)[source]#
Get tables sorted by topological dependency order using SQLite catalog.
- Return type:
- get_columns(driver, table=None, schema=None)[source]#
Get column information for a table or schema.
- Return type:
- get_indexes(driver, table=None, schema=None)[source]#
Get index metadata for a table or schema.
- Return type:
Extension Settings#
Use the configuration types below in their corresponding extension_config
namespace: "litestar", "events", or "adk" as supported by this adapter.
- class sqlspec.adapters.sqlite.litestar.SqliteLitestarConfig[source]#
Bases:
LitestarConfigSqlite-specific Litestar settings.
Use inside
extension_config["litestar"]with this adapter's session store.- pragma_profile: NotRequired[bool]#
False.
- Type:
Apply the extension-store PRAGMA profile. Default
- class sqlspec.adapters.sqlite.events.SqliteEventsConfig[source]#
Bases:
EventsConfigSqlite events settings for queue storage and supported native transports.
- pragma_profile: NotRequired[bool]#
False.
- Type:
Apply the SQLite extension-store PRAGMA profile. Default
- class sqlspec.adapters.sqlite.adk.SqliteADKConfig[source]#
Bases:
ADKConfigSQLite-specific ADK extension settings.
Use these keys inside
extension_config["adk"]with SQLite ADK stores.- pragma_overrides: NotRequired[Mapping[str, str | int | bool]]#
Additional validated PRAGMA settings applied after the built-in ADK profile.
- fts_tokenize: NotRequired[str]#
Optional FTS5 tokenizer spec used when
memory_use_ftsis enabled.
- fts_detail: NotRequired[Literal['full', 'column', 'none']]#
Optional FTS5 detail mode used when
memory_use_ftsis enabled.