Skip to main content

Database

Database connection manager.

Usage

Source

Database(
    path,
    *,
    echo=False,
)

Attributes

path

Path to SQLite database file.

engine
SQLAlchemy engine instance.

Methods

Name Description
__enter__() Return this database, to be closed on exiting the context.
__exit__() Close the database connection.
__init__() Initialize database connection manager.
close() Close the database connection.
session() Return a fresh session bound to this database’s engine.

__enter__()

Return this database, to be closed on exiting the context.

Usage

Source

__enter__()

__exit__()

Close the database connection.

Usage

Source

__exit__(exc_type, exc_value, traceback)

__init__()

Initialize database connection manager.

Usage

Source

__init__(path, *, echo=False)
Parameters
path: str | Path

Path to the SQLite database file.

echo: bool = False
If True, SQL statements will be logged to the standard output. If False, no logging is performed.

close()

Close the database connection.

Usage

Source

close()

session()

Return a fresh session bound to this database’s engine.

Usage

Source

session()
Note

A new session is created per call; use it as a context manager (with database.session() as session: ...) to close it on exit. Nothing is committed automatically — call session.commit() explicitly. Validation and automatic identities are only applied by sessions of type AutostorageSession, such as the ones returned here.