Skip to main content

Python bindings for SQLRite — a small, embeddable SQLite clone written in Rust.

Project description

sqlrite (Python)

Python bindings for SQLRite — a small, embeddable SQLite clone written in Rust. Shape follows PEP 249 / the stdlib sqlite3 module, so most callers can pick it up without reading the docs.

Install

# From PyPI:
pip install sqlrite

# From source in a clone of the repo:
pip install maturin
cd sdk/python
maturin develop --release

Quick tour

import sqlrite

# File-backed or in-memory (use `":memory:"` to match sqlite3 convention).
conn = sqlrite.connect("foo.sqlrite")
# conn = sqlrite.connect(":memory:")

cur = conn.cursor()
cur.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
cur.execute("INSERT INTO users (name, age) VALUES ('alice', 30)")
cur.execute("INSERT INTO users (name, age) VALUES ('bob', 25)")

cur.execute("SELECT id, name, age FROM users")
for row in cur:
    print(row)  # (1, 'alice', 30), then (2, 'bob', 25)

# Column metadata (PEP 249 `.description`):
for col in cur.description:
    print(col[0])  # 'id', 'name', 'age'

conn.close()

Transactions

with sqlrite.connect("foo.sqlrite") as conn:
    cur = conn.cursor()
    cur.execute("BEGIN")
    cur.execute("INSERT INTO users (name) VALUES ('carol')")
    if looks_good:
        conn.commit()
    else:
        conn.rollback()
# The context manager automatically commits on clean exit
# and rolls back on exception, then closes the connection.

Read-only access

conn = sqlrite.connect_read_only("foo.sqlrite")
# Any write raises sqlrite.SQLRiteError. Multiple read-only
# connections on the same file coexist (shared OS lock).

Vector columns + KNN (Phase 7a–7d)

The engine ships with a fixed-dimension VECTOR(N) storage class and three distance functions (vec_distance_l2, vec_distance_cosine, vec_distance_dot). Plain cursor.execute(...) is all you need from Python — values come back as Python lists of floats.

cur.execute("CREATE TABLE docs (id INTEGER PRIMARY KEY, embedding VECTOR(384))")
cur.execute("INSERT INTO docs (id, embedding) VALUES (1, [0.1, 0.2, ..., 0.0])")  # 384 floats
cur.execute("""
    SELECT id FROM docs
     ORDER BY vec_distance_l2(embedding, [0.1, 0.2, ..., 0.0])
     LIMIT 10
""")
for row in cur:
    print(row)

For larger collections, build an HNSW index — the executor will use it automatically when the WHERE/ORDER BY shape matches:

cur.execute("CREATE INDEX idx_docs_emb ON docs USING hnsw (embedding)")

JSON columns (Phase 7e)

JSON (and JSONB as an alias) columns store text, validated at INSERT/UPDATE time. Read with json_extract / json_type / json_array_length / json_object_keys. Path subset: $, .key, [N], chained.

cur.execute("CREATE TABLE events (id INTEGER PRIMARY KEY, payload JSON)")
cur.execute(
    "INSERT INTO events (payload) VALUES "
    "('{\"user\": {\"name\": \"alice\"}, \"score\": 42}')"
)
cur.execute(
    "SELECT json_extract(payload, '$.user.name'), json_type(payload, '$.score') FROM events"
)
print(cur.fetchall())  # [('alice', 'integer')]

json_object_keys returns a JSON-array text rather than a table-valued result (set-returning functions aren't supported yet).

Natural-language → SQL (Phase 7g.4)

Connection.ask(question) generates SQL from a natural-language question via the configured LLM provider (Anthropic by default). Connection.ask_run(question) is the convenience that calls ask() then immediately executes the generated SQL.

import sqlrite

conn = sqlrite.connect("foo.sqlrite")
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
conn.execute("INSERT INTO users (name, age) VALUES ('alice', 30)")

# Reads SQLRITE_LLM_API_KEY from the environment.
resp = conn.ask("How many users are over 30?")
print(resp.sql)            # "SELECT COUNT(*) FROM users WHERE age > 30"
print(resp.explanation)    # "Counts users over the age threshold."

# Caller decides whether to run the SQL — ask() does NOT auto-execute.
cur = conn.cursor()
cur.execute(resp.sql)
print(cur.fetchall())      # [(1,)]

# Or one-shot:
rows = conn.ask_run("How many users are over 30?").fetchall()

Configuration

Three precedence layers — explicit-wins:

  1. Per-call config: conn.ask("...", sqlrite.AskConfig(api_key="..."))
  2. Per-connection config: conn.set_ask_config(cfg) — applies to all subsequent ask() / ask_run() calls on this connection
  3. Environment vars (zero-config fallback): SQLRITE_LLM_PROVIDER / _API_KEY / _MODEL / _MAX_TOKENS / _CACHE_TTL
# Path 1: env (zero config) — same env vars as REPL/Desktop
resp = conn.ask("how many users?")

# Path 2: explicit per-call (highest precedence)
cfg = sqlrite.AskConfig(
    api_key="sk-ant-...",
    model="claude-haiku-4-5",
    max_tokens=512,
    cache_ttl="1h",        # "5m" (default) | "1h" | "off"
)
resp = conn.ask("how many users?", cfg)

# Path 3: per-connection (set once, reuse)
conn.set_ask_config(cfg)
resp = conn.ask("how many users?")     # uses cfg
resp = conn.ask("count by age")        # uses cfg

# Or build from env explicitly:
cfg = sqlrite.AskConfig.from_env()

Defaults

provider="anthropic", model="claude-sonnet-4-6", max_tokens=1024, cache_ttl="5m". The schema dump goes inside an Anthropic prompt-cache breakpoint — repeat asks against the same DB hit the cache (verify via resp.usage.cache_read_input_tokens).

Errors

Missing API key → sqlrite.SQLRiteError("missing API key (set SQLRITE_LLM_API_KEY ...)"). API errors (4xx/5xx) bubble up as SQLRiteError with the status code + Anthropic's structured error message. ask_run() on an empty SQL response (model declined to generate) raises with the model's explanation rather than executing the empty string.

What AskResponse carries

resp.sql              # str — generated SQL, ready to execute (or '' if model declined)
resp.explanation      # str — one-sentence rationale (may be empty)
resp.usage.input_tokens
resp.usage.output_tokens
resp.usage.cache_creation_input_tokens
resp.usage.cache_read_input_tokens

AskConfig.__repr__ and AskResponse.__repr__ deliberately don't include the API key — printing config in logs won't leak it.

API surface

Function / Method Purpose
sqlrite.connect(db) Open or create a file-backed DB (or :memory:)
sqlrite.connect_read_only(db) Open an existing file with a shared lock
Connection.cursor() Returns a new Cursor
Connection.execute(sql, ...) Shortcut for cursor().execute(...)
Connection.commit() / .rollback() Close the current transaction
Connection.close() Drop the connection (releases OS file lock)
Connection.in_transaction bool — inside a BEGIN … COMMIT/ROLLBACK
Connection.read_only bool — opened via connect_read_only
Cursor.execute(sql, params=None) Run one statement
Cursor.executemany(sql, seq) DB-API placeholder (params deferred)
Cursor.executescript(sql) ;-separated batch of statements
Cursor.fetchone() Next row as a tuple, or None
Cursor.fetchmany(size=None) Up to size more rows as a list of tuples
Cursor.fetchall() All remaining rows
Cursor.description PEP 249 7-tuples per column (name + Nones)
Cursor.rowcount -1 (not tracked yet — returns as PEP 249 says)
iter(cursor) for row in cursor: …
sqlrite.SQLRiteError All engine failures bubble up as this

Parameter binding

execute(sql, params) accepts None and empty tuples/lists for DB-API compatibility, but any non-empty params raises TypeError with a clear message — the underlying engine doesn't support prepared-statement parameter binding yet (deferred to Phase 5a.2). For now, inline values into the SQL (with manual escaping).

Running the tests

maturin develop
python -m pytest tests/

How this ships

  • PyO3 (abi3-py38) for the Rust-Python boundary — one wheel works on every CPython 3.8+ release, no per-version rebuild.
  • maturin as the build backend, emitting standard .whl files that pip can install directly.
  • Phase 6f's CI publishes abi3-py38 wheels to PyPI for manylinux x86_64/aarch64, macOS aarch64, and Windows x86_64 (plus an sdist) on every release. OIDC trusted publishing — no long-lived PyPI token in the repo.

Status

Phase 5c MVP: ✅ — basic CRUD, transactions, context managers, read-only mode, iteration. Parameter binding, CursorRow namedtuples, and type converters (datetime, Decimal) are natural follow-ups.

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

sqlrite-0.1.24.tar.gz (896.7 kB view details)

Uploaded Source

Built Distributions

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

sqlrite-0.1.24-cp38-abi3-win_amd64.whl (4.7 MB view details)

Uploaded CPython 3.8+Windows x86-64

sqlrite-0.1.24-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

sqlrite-0.1.24-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.0 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

sqlrite-0.1.24-cp38-abi3-macosx_11_0_arm64.whl (4.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file sqlrite-0.1.24.tar.gz.

File metadata

  • Download URL: sqlrite-0.1.24.tar.gz
  • Upload date:
  • Size: 896.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlrite-0.1.24.tar.gz
Algorithm Hash digest
SHA256 2285c9ff763ac645411eb904c423b696953eda9c4ccd5b76c793ade5e4f35793
MD5 6df47b7e801d03312a56c5534e8a3fab
BLAKE2b-256 f7f35b0d3a6e42928cb140ff70e1d130d70b0e87488ede05107ef510d02344bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlrite-0.1.24.tar.gz:

Publisher: release.yml on joaoh82/rust_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlrite-0.1.24-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: sqlrite-0.1.24-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 4.7 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlrite-0.1.24-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4f942245ac9af0e865222d5f0e34f289b8b4597c86c5f61044973d833e1b31f1
MD5 288331535041994f266fcd1063e3ebef
BLAKE2b-256 8a2a019126e97ca25c98fa418df080d62d8a295b45e7077d59dd4962aa7cfbac

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlrite-0.1.24-cp38-abi3-win_amd64.whl:

Publisher: release.yml on joaoh82/rust_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlrite-0.1.24-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sqlrite-0.1.24-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8a96ed3f978c31b98047c6597a950c88ef9305270ce1498b4e90a07f36312e2e
MD5 2641a31ee680715d9127724a47600ab1
BLAKE2b-256 f2bb24fac77b358bc0f056c2db2ac73df4475c1fc98983cda5bfd5ec760e56d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlrite-0.1.24-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on joaoh82/rust_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlrite-0.1.24-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sqlrite-0.1.24-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 36e589c509aedd8f358d6a8e6ee49b6eda9951be339ec269aa4b21828d6cdbb1
MD5 0d2a5113935fb41b9c6fb659c94ab074
BLAKE2b-256 4190c9b84ceba14ef500ee3d27eedd7b62267088b3a9de1ec08546fe5e76300d

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlrite-0.1.24-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on joaoh82/rust_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlrite-0.1.24-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sqlrite-0.1.24-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cbfd3361b91d02fde9e93a0b286d06ac6eb9aef6979975301bcc9e34aa899cc0
MD5 3f5a0f1c06496d7eb1654ea31877cb32
BLAKE2b-256 98ab90ef4e3fa6d0ba5d3edc522dd7d92888fee7411a58309c73b23f0708e113

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlrite-0.1.24-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on joaoh82/rust_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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