Skip to main content

Using dataclasses with SQL databases.

Project description


GitHub Workflow Status PyPI

Using dataclasses with SQL databases.


import dataclasses
import sqlalchemy
import dataclasses_sql

class Car:
    brand: str = dataclasses.field(metadata={"key": True})
    model: str = dataclasses.field(metadata={"key": True})
    milage: float

# Connect to database
engine = sqlalchemy.create_engine("sqlite:///:memory:")
metadata = sqlalchemy.MetaData(engine)

# Insert
car = Car("Kia", "Ceed", 15678)
dataclasses_sql.insert(metadata, car, check_exists=True)

car = Car("Ford", "Mustang", 4032)
dataclasses_sql.insert(metadata, car, check_exists=True)

# Select
builder = dataclasses_sql.SelectStatementBuilder()
builder.add_column(Car, "mileage")
builder.add_clause(Car, "brand", "Kia")
statement =

with metadata.bind.begin() as conn:
    row = conn.execute(statement).fetchone()


Easiest way to install using pip:

pip install dataclasses-sql

For development installation from the git repository::

git clone
cd dataclasses-sql
pip install -e .

Release notes


  • Add delete function


  • Add update function



The library is provided under the MIT license license.

Copyright (c) 2020, Philippe Pinard

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

dataclasses-sql-0.3.0.tar.gz (24.0 kB view hashes)

Uploaded source

Built Distribution

dataclasses_sql-0.3.0-py3-none-any.whl (14.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page