Skip to main content

Using dataclasses with SQL databases.

Project description


Build Status

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




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.

Files for dataclasses-sql, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size dataclasses_sql-0.1.1-py3-none-any.whl (7.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size dataclasses-sql-0.1.1.tar.gz (23.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page