Skip to main content

sqlclz

A type-safe Python SQL query builder that uses NamedTuple classes to define database schemas and generate SQL statements.

Installation

pip install sqlclz

Quick Start

Define Your Schema

from typing import NamedTuple, Annotated
from sqlclz import named_tuple_table_class, PRIMARY


@named_tuple_table_class
class Person(NamedTuple):
    name: Annotated[str, PRIMARY()]
    age: int
    email: str

Create Tables and Insert Data

import sqlclz
from sqlclz import Connection

# Create a connection (in-memory SQLite database)
with Connection() as conn:
    # Create table
    conn.execute(sqlclz.create_table(Person))

    # Insert data
    conn.execute(
        sqlclz.insert_into(Person),
        [("Alice", 30, "alice@example.com"),
         ("Bob", 25, "bob@example.com")]
    )

    # Query data
    result = conn.execute(
        sqlclz.select_from(Person).where(Person.age > 25)
    ).fetchall()

    print(result)
    # Output: [('Alice', 30, 'alice@example.com')]

Type-Safe Queries

# SELECT with specific fields
query = sqlclz.select_from(Person.name, Person.email)

# WHERE conditions with type safety
query = sqlclz.select_from(Person).where(
    Person.age >= 18,
    Person.age <= 65
)

# ORDER BY
query = sqlclz.select_from(Person).order_by(sqlclz.desc(Person.age))

# LIMIT and OFFSET
query = sqlclz.select_from(Person).limit(10, offset=5)

Advanced Features

Foreign Keys

from sqlclz import foreign


@named_tuple_table_class
class Department(NamedTuple):
    id: Annotated[int, PRIMARY(auto_increment=True)]
    name: str


@named_tuple_table_class
class Employee(NamedTuple):
    id: Annotated[int, PRIMARY(auto_increment=True)]
    name: str
    dept_id: int

    @foreign(Department)
    def _department(self):
        return self.dept_id

Joins

# Join tables
query = sqlclz.select_from(
    Employee.name,
    Department.name
).join(
    Employee.dept_id == Department.id,
    by='inner'
)

Aggregations

# Count, sum, avg, etc.
query = sqlclz.select_from(
    sqlclz.count(Person.name),
    sqlclz.avg(Person.age)
).group_by(Person.email)

Features

  • Type-Safe: Leverage Python's type system to catch errors at development time
  • Pythonic: Define schemas using familiar NamedTuple syntax
  • SQL Generation: Automatically generate correct SQL statements
  • Foreign Keys: Support for foreign key relationships
  • Comprehensive: Supports SELECT, INSERT, UPDATE, DELETE, JOIN, and more
  • Window Functions: Support for advanced SQL window functions
  • CTE Support: Common Table Expressions (WITH clause)

Running Tests

# Install test dependencies
cd test/unit
curl -L -o chinook.zip https://www.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip
unzip chinook.zip

# Run tests
python -m unittest discover -s test/unit -p "test*.py"

Documentation

For more examples and detailed documentation, see the test directory.

License

BSD 3-Clause

Metadata

Release files for sqlclz 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlclz 1.0.0
File Size Uploaded Provenance Actions
sqlclz-1.0.0.tar.gz 42.3 kB With provenance Details for sqlclz-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlclz 1.0.0
File Interpreter ABI Platform Actions
sqlclz-1.0.0-py3-none-any.whl Python 3 none any Details for sqlclz-1.0.0-py3-none-any.whl

Total release size: 89.8 kB

Release files / sqlclz-1.0.0.tar.gz

Download URL sqlclz-1.0.0.tar.gz
Size 42.3 kB
Tags Source
SHA-256 checksum
How to use checksums
54c4ed070bab704d498c858ea5a60e26cfbc58b103ee6ea1bb97df4111062704
BLAKE2b-256 checksum
How to use checksums
bf03cfb73e2f1684b0f4866503e9a57ef1de8bedce37ceb50daf772c2f4d60ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 11, 2026.

Transparency log

Release files / sqlclz-1.0.0-py3-none-any.whl

Download URL sqlclz-1.0.0-py3-none-any.whl
Size 47.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
447c5904a74bd791fb89a7cf06ae57cdb6caab8029e7fa868a0424f52b87940d
BLAKE2b-256 checksum
How to use checksums
b1a344f1273245db496b670de6477bb98640d7d3548489ca6cc9cd9709361a50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 11, 2026.

Transparency log

Security for sqlclz 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sqlclz-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page