SQLite Database¶
sqlite ¶
Classes¶
SQLitePoolConfig ¶
SQLite ¶
SQLite(uri: str, *, tables: list[type[SQLModel]] | None = None, echo: bool = False, pool_size: int = 20, max_overflow: int = 10, pool_timeout: float = 30.0, pool_recycle: int = 3600, pool_pre_ping: bool = True, create_all: bool = True)
Bases: Database
SQLite database container with async SQLModel/SQLAlchemy support.
This class provides a high-level interface for SQLite database operations with the following features:
- Async engine and session management for repository pattern
- Connection pooling with configurable parameters
- Raw SQL execution with transaction control
- Schema management utilities (create_all/drop_all)
- Unified lifecycle management through AsyncContextMixin
Attributes:
| Name | Type | Description |
|---|---|---|
uri | SQLite connection URI. | |
engine | AsyncEngine | None | SQLAlchemy async engine (initialized after init()). |
sessionmaker | async_sessionmaker[AsyncSession] | None | Async session factory (initialized after init()). |
cfg | Connection pool configuration. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri | str | SQLite connection URI (must use aiosqlite driver). Example: "sqlite+aiosqlite:///./database.db" (relative path) Example: "sqlite+aiosqlite:////absolute/path/database.db" (absolute path) Example: "sqlite+aiosqlite:///:memory:" (in-memory database) | required |
tables | list[type[SQLModel]] | None | List of SQLModel classes to manage. Used for create_all/drop_all. | None |
echo | bool | Whether to log all SQL statements (useful for debugging). | False |
pool_size | int | Number of connections to maintain in the pool. Note: SQLite with aiosqlite uses NullPool by default in async mode. | 20 |
max_overflow | int | Max number of connections beyond pool_size. | 10 |
pool_timeout | float | Seconds to wait before timing out on connection. | 30.0 |
pool_recycle | int | Seconds after which to recycle connections. Set to -1 to disable recycling. | 3600 |
pool_pre_ping | bool | Test connections before using them. Recommended for production to handle stale connections. | True |
create_all | bool | Whether to create all tables on init(). | True |
Example
# Setup with file-based database
sqlite = SQLite(
uri="sqlite+aiosqlite:///./app.db",
tables=[User, Post],
echo=True,
)
# Setup with in-memory database (useful for testing)
sqlite = SQLite(
uri="sqlite+aiosqlite:///:memory:",
tables=[User, Post],
)
# Initialize
await sqlite.init()
# Create tables
await sqlite.create_all()
# Use session for ORM operations
async with sqlite.session() as session:
user = await session.get(User, user_id)
user.username = "new_name"
await session.commit()
# Execute raw SQL
result = await sqlite.exec(
"SELECT * FROM users WHERE age > :age",
readonly=True,
age=21,
)
# Cleanup
await sqlite.close()
Note
- The URI must use the aiosqlite driver for async support.
- SQLite doesn't support some PostgreSQL features (e.g., JSONB operators).
- Use JSON1 extension for JSON operations (enabled by default).
- File path format: Use three slashes for relative paths, four for absolute.
Source code in audex/lib/database/sqlite.py
Functions¶
init async ¶
Initialize the database engine and session factory.
This method creates the async engine with connection pooling and sets up the session factory. It should be called during application startup, typically in a lifespan context manager.
For SQLite, this also enables foreign key constraints and loads the JSON1 extension if available.
Raises:
| Type | Description |
|---|---|
Exception | If engine creation fails (e.g., invalid URI). |
Source code in audex/lib/database/sqlite.py
close async ¶
Close the database engine and clean up resources.
This method disposes of the connection pool and resets the engine and session factory. It should be called during application shutdown.
Note
This method is idempotent and safe to call multiple times.
Source code in audex/lib/database/sqlite.py
session ¶
Create a new async database session.
Returns:
| Type | Description |
|---|---|
AsyncSession | An async session context manager. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If sessionmaker is not initialized (call init() first). |
Example
Note
The session is automatically committed on successful exit and rolled back on exception. You can also manually commit/rollback within the context.
Source code in audex/lib/database/sqlite.py
exec async ¶
Execute a raw SQL statement.
This method provides direct SQL execution for cases where ORM abstractions are insufficient or when specific optimizations are needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql | str | Raw SQL string to execute. Use named parameters with colon prefix. | required |
readonly | bool | If True, does not commit the transaction. Use this for SELECT queries to avoid unnecessary commits. | False |
**params | Any | Named parameters for the SQL statement. | {} |
Returns:
| Type | Description |
|---|---|
Result[Any] | SQLAlchemy Result object containing query results. |
Raises:
| Type | Description |
|---|---|
RuntimeError | If execution fails, with the original exception as the cause. |
Example
# Read-only query
result = await sqlite.exec(
"SELECT * FROM users WHERE age > :age",
readonly=True,
age=21,
)
users = result.fetchall()
# Write query
await sqlite.exec(
"UPDATE users SET status = :status WHERE id = :id",
readonly=False,
status="active",
id=123,
)
# Using JSON1 extension
result = await sqlite.exec(
"SELECT * FROM users WHERE json_extract(tags, '$.premium') = 1",
readonly=True,
)
Warning
Be careful with SQL injection. Always use parameterized queries with named parameters instead of string formatting.
Source code in audex/lib/database/sqlite.py
ping async ¶
Check database connectivity.
This method attempts to execute a simple query to verify that the database is reachable and responsive.
Returns:
| Type | Description |
|---|---|
bool | True if database is reachable, False otherwise. |
Note
This method does not raise exceptions. It catches all errors and returns False instead.
Source code in audex/lib/database/sqlite.py
create_all async ¶
Create all database tables.
This method creates tables for the specified SQLModel classes, or all tables in the SQLModel metadata if no models are specified.
Raises:
| Type | Description |
|---|---|
RuntimeError | If engine is not initialized. |
Example
Warning
This is typically used for development/testing. In production, use proper migration tools like Alembic to manage schema changes.
Source code in audex/lib/database/sqlite.py
drop_all async ¶
Drop all database tables.
This method drops all tables defined in the SQLModel metadata.
Raises:
| Type | Description |
|---|---|
RuntimeError | If engine is not initialized. |
Warning
This is destructive and should only be used in development/testing. All data will be lost. There is no confirmation prompt.
Source code in audex/lib/database/sqlite.py
vacuum async ¶
Run VACUUM command to optimize the database file.
This command rebuilds the database file, repacking it into a minimal amount of disk space. It's useful after deleting large amounts of data.
Raises:
| Type | Description |
|---|---|
RuntimeError | If engine is not initialized. |
Example
Note
VACUUM requires exclusive access to the database and may take significant time on large databases.
Source code in audex/lib/database/sqlite.py
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true