Useful wrapper around SQLite
Project description
SQLitey
SQLitey is a lightweight and flexible wrapper around SQLite, designed to streamline database access using configuration files, SQL templates, and custom row factories.
Key Features:
- Configuration-driven setup for database paths and SQL templates
- Support for SQL template files to keep queries organized
- Customizable row factories (e.g., return rows as namedtuples)
- Support for both templated and raw SQL queries
- Optional config usage for quick, one-off database access
Exampe usage
Using a Config File
Define a configuration with the database path and the directory for SQL templates:
from pathlib import Path
from sqlitey import Db, DbPathConfig, Sql, namedtuple_factory
dir = Path(__file__).resolve().parent
config = DbPathConfig(
database=dir / "db.sqlite3",
sql_templates_dir=dir / "sql"
)
with Db.from_config(config, row_factory=namedtuple_factory) as db:
result = db.fetchone(Sql.template("get_user_by_id.sql"), (3,))
print(result.id, result.name)
Executing Raw SQL
You can also use raw SQL directly:
with Db.from_config(config, timeout=15) as db:
db.commit(Sql.raw("INSERT INTO users VALUES (10, 'John');"))
Without a Config File
Skip the config and use the database path directly:
with Db("db.sqlite3", autocommit=True) as db:
db.execute(Sql.raw("UPDATE users SET name = 'Johnny' WHERE id = 10;"))
with Db("db.sqlite3") as db:
sql = Sql.template("get_user_by_id.sql", path=Path(__file__).resolve().parent / "sql")
result = db.fetchone(sql, (3,))
More examples
See more examples in tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlitey-0.1.0.tar.gz.
File metadata
- Download URL: sqlitey-0.1.0.tar.gz
- Upload date:
- Size: 40.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86a47e44274936203934f2a5a18e1d3477661292713777f77eba921284da37b8
|
|
| MD5 |
c04eccadf40b84e2e9a60881880f8665
|
|
| BLAKE2b-256 |
71054bf72f68e6bf7a2a16b0f1ba506cad38ecc909371390c8aeef60b5a3fce1
|
File details
Details for the file sqlitey-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlitey-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
103c3b965b7e69baf067383b61967ff1e43f74f80ef2c69ce077ce284d4e9beb
|
|
| MD5 |
6d7480e2a7ac7368027ca4af5aa745a9
|
|
| BLAKE2b-256 |
50e504864d8c9675b35b521155bfea359378405d46b76594373ebf42f6718f99
|