Skip to main content

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

Installation

pip install sqlitey

Quick Start

from sqlitey import Db, Sql

with Db("mydb.sqlite") as db:
    db.commit(Sql.raw("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"))
    db.commit(Sql.raw("INSERT INTO users (name) VALUES (?)"), ("Alice",))
    user = db.fetchone(Sql.raw("SELECT * FROM users WHERE id = ?"), (1,))
    print(user)  # (1, 'Alice')

Usage

Row Factories

Return rows as dictionaries or namedtuples instead of tuples:

from sqlitey import Db, Sql, dict_factory, namedtuple_factory

with Db("mydb.sqlite", row_factory=dict_factory) as db:
    user = db.fetchone(Sql.raw("SELECT * FROM users WHERE id = ?"), (1,))
    print(user["name"])  # 'Alice'

with Db("mydb.sqlite", row_factory=namedtuple_factory) as db:
    user = db.fetchone(Sql.raw("SELECT * FROM users WHERE id = ?"), (1,))
    print(user.name)  # 'Alice'

SQL Templates

Keep SQL queries in separate files for better organization:

from pathlib import Path
from sqlitey import Db, DbPathConfig, Sql

config = DbPathConfig(
    database=Path("mydb.sqlite"),
    sql_templates_dir=Path("sql/"),
)

with Db.from_config(config) as db:
    # Loads query from sql/get_user_by_id.sql
    user = db.fetchone(Sql.template("get_user_by_id.sql"), (1,))

Or specify the template path directly:

with Db("mydb.sqlite") as db:
    sql = Sql.template("get_user_by_id.sql", path=Path("sql/"))
    user = db.fetchone(sql, (1,))

Autocommit Mode

Disable transaction management for auto-committing each statement:

with Db("mydb.sqlite", autocommit=True) as db:
    db.execute(Sql.raw("UPDATE users SET name = ? WHERE id = ?"), ("Bob", 1))

Batch Operations

Execute multiple parameter sets or run SQL scripts:

with Db("mydb.sqlite", autocommit=True) as db:
    # Execute same query with multiple parameter sets
    users = [("Alice",), ("Bob",), ("Charlie",)]
    db.executemany(Sql.raw("INSERT INTO users (name) VALUES (?)"), users)

    # Execute multiple statements as a script
    db.executescript(Sql.raw("""
        DELETE FROM users WHERE id = 1;
        UPDATE users SET name = 'Robert' WHERE name = 'Bob';
    """))

API Reference

Sql

Method Description
Sql.raw(query) Create a Sql instance from an inline query string
Sql.template(filename, path=None) Create a Sql instance from a template file

Db

Method Description
Db(path, row_factory=None, sql_templates_dir=None, autocommit=False) Create a database connection
Db.from_config(config, **kwargs) Create from a DbPathConfig
execute(sql, *args) Execute a query and return the cursor
executemany(sql, *args) Execute a query against multiple parameter sets
executescript(sql) Execute multiple statements as a script
fetchone(sql, *args) Execute and return the first row
fetchall(sql, *args) Execute and return all rows
commit(sql, *args) Execute and commit the transaction

Row Factories

Factory Description
dict_factory Return rows as dictionaries
namedtuple_factory Return rows as namedtuples

More Examples

See the test suite for more examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlitey-0.1.1.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlitey-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlitey-0.1.1.tar.gz.

File metadata

  • Download URL: sqlitey-0.1.1.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.14

File hashes

Hashes for sqlitey-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2ea08af7764a372d73cd8dd29c086a2ff9474ffcf1d5da9ac47fcc564d0f8550
MD5 0d0936cde01bbeff26104e976b8dc5e1
BLAKE2b-256 752b4d556a871c92c98e26a32fe22fa4792442108f30898f2f6f3b9cbaeb2d42

See more details on using hashes here.

File details

Details for the file sqlitey-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sqlitey-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.14

File hashes

Hashes for sqlitey-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ae820b3f589c784b024081e482e9e2d74d7ffb8e38bb5049ef6ba5a18156258
MD5 fb68a0dee661ca4e4a2037ee811cd564
BLAKE2b-256 88be26e30042f7f7a20da7a0424a1998f909e3b083947f62482878f889d461c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page