Connection helpers
Helpers for building SQLite URIs, opening connections, and bootstrapping schema.
create_read_only_connection(db_path: str | Path) -> sqlite3.Connection
¶
Create a read-only SQLite connection for an existing database.
The caller is responsible for closing the connection when done.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Path to an existing SQLite database file. |
required |
Returns:
| Type | Description |
|---|---|
sqlite3.Connection
|
Open SQLite connection configured with sqlite3.Row row factory. |
Notes
The target database is expected to already contain the required schema. Read-only connections cannot create temporary tables. Query helpers that rely on temporary tables for large key filters may fail in this mode.
Source code in src/pfmsoft/eve_snippets/sqlite3/connection_helpers.py
create_read_write_connection(db_path: str | Path, init_sql: str | None = None) -> sqlite3.Connection
¶
Create a read-write SQLite connection and optionaly ensure the packaged schema exists.
If the database file does not exist, it will be created. If init_sql is provided, it will be executed to create any missing tables or other schema objects.
The caller is responsible for closing the connection when done.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Path to the SQLite database file. |
required |
init_sql
|
str | None
|
Optional SQL script to execute on the connection to ensure schema exists. |
None
|
Returns:
| Type | Description |
|---|---|
sqlite3.Connection
|
Open SQLite connection configured with sqlite3.Row row factory. |
Notes
Missing tables and other schema objects defined in the packaged SQL script are created before the connection is returned.
Source code in src/pfmsoft/eve_snippets/sqlite3/connection_helpers.py
db_connection_manager(db_path: str | Path, init_sql: str | None = None, read_only: bool = True) -> Iterator[sqlite3.Connection]
¶
Yield a SQLite connection and close it automatically on exit.
Delegates to the read-only or read-write connection factory based on the read_only flag.
read_only connections are expected to be used with existing databases that already contain the required schema. read-write connections will create the database file if it does not exist and will execute the init_sql script to create any missing tables or other schema objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Path to the SQLite database file. |
required |
init_sql
|
str | None
|
Optional SQL script to execute on the connection to ensure schema exists. |
None
|
read_only
|
bool
|
Whether to open the connection in read-only mode. |
True
|
Yields:
| Type | Description |
|---|---|
sqlite3.Connection
|
Open SQLite connection configured with sqlite3.Row row factory. |
Source code in src/pfmsoft/eve_snippets/sqlite3/connection_helpers.py
read_only_uri(db_path: str | Path) -> str
¶
Build a read-only SQLite URI for use with sqlite3.connect(uri=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Filesystem path to the SQLite database. |
required |
Returns:
| Type | Description |
|---|---|
str
|
URI string with mode=ro. |
Source code in src/pfmsoft/eve_snippets/sqlite3/connection_helpers.py
read_write_uri(db_path: str | Path) -> str
¶
Build a read-write/create SQLite URI for use with sqlite3.connect(uri=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str | Path
|
Filesystem path to the SQLite database. |
required |
Returns:
| Type | Description |
|---|---|
str
|
URI string with mode=rwc. |