Skip to content

Database Operations

Functions for creating and managing database connections.

create_database_with_schema

python
create_database_with_schema((db_path: str) -> None)

Create a new database and apply the complete schema.

Parameters:

  • db_path: Path where the new database should be created

Raises:

  • DatabaseError: If schema files cannot be found or applied

database_context

python
database_context((db_path: str, read_only: bool = False) -> Generator[sqlite3.Connection, NoneType, NoneType])

Context manager function for database connections.

Parameters:

  • db_path: Path to the SQLite database file
  • read_only: If True, open in read-only mode
  • Yields:
  • sqlite3.Connection: Database connection with proper configuration

Example:

python
with database_context("model.db") as conn:
        cursor = conn.execute("SELECT * FROM networks")
        networks = cursor.fetchall()

open_connection

python
open_connection((db_path: str, read_only: bool = False) -> sqlite3.Connection)

Open database connection with proper settings.

Parameters:

  • db_path: Path to the SQLite database file
  • read_only: If True, open in read-only mode

Returns:

sqlite3.Connection: Configured database connection

Raises:

  • ConnectionError: If database connection fails

validate_database

python
validate_database((conn: sqlite3.Connection) -> None)

Validate database schema has required tables.

Parameters:

  • conn: Database connection to validate

Raises:

  • ValidationError: If required tables are missing

© Copyright 2025 Bayesian Energy