sqldol
SQL (through SQLAlchemy) with a simple (dict-like or list-like) interface
Installation
pip install sqldol
Quick Start
Basic Usage with SQLAlchemyStore
from sqldol import SQLAlchemyStore, SQLAlchemyPersister # the TYPE_* constants live on the persister
# Connect to an in-memory SQLite database
store = SQLAlchemyStore(
uri='sqlite:///:memory:',
collection_name='users',
key_fields={'user_id': SQLAlchemyPersister.TYPE_INTEGER},
data_fields={'name': SQLAlchemyPersister.TYPE_STRING, 'email': SQLAlchemyPersister.TYPE_STRING}
)
# Create
store[{'user_id': 1}] = {'name': 'Alice', 'email': 'alice@acme.dev'}
# Read
user = store[{'user_id': 1}]
print(user.name) # Alice
# Update
store[{'user_id': 1}] = {'name': 'Alice Smith', 'email': 'alice.smith@acme.dev'}
# Delete
del store[{'user_id': 1}]
Working with Different Store Types
The Sql*Reader/SqlDictStore examples below read a table through a SQLAlchemy engine. Here is a small SQLite table to try them on:
import sqlalchemy as sa
engine = sa.create_engine('sqlite:///:memory:')
with engine.begin() as conn:
conn.execute(sa.text("CREATE TABLE my_table (id TEXT, name TEXT, age INTEGER, category TEXT, price REAL)"))
conn.execute(sa.text("""INSERT INTO my_table VALUES
('user123', 'John', 30, 'people', NULL),
('1', 'Phone', NULL, 'electronics', NULL),
('2', 'Laptop', NULL, 'electronics', NULL),
('123', 'Widget', NULL, 'tools', 19.99)"""))
SqlDictStore - Dictionary Values
from sqldol import SqlDictStore
# Returns single dictionaries as values
dict_store = SqlDictStore(engine, 'my_table', key_columns='id', value_columns=['name', 'age'])
# Access returns a dictionary
user_data = dict_store['user123'] # {'name': 'John', 'age': 30}
SqlRowsReader - List of Rows
from sqldol import SqlRowsReader
# Returns lists of rows (each row a list)
rows_reader = SqlRowsReader(engine, 'my_table', key_columns='category', value_columns=['id', 'name'])
# Access returns a list of rows
items = rows_reader['electronics'] # [['1', 'Phone'], ['2', 'Laptop']]
SqlRowReader - Single Row
from sqldol import SqlRowReader
# Returns a single row (a list)
row_reader = SqlRowReader(engine, 'my_table', key_columns='id', value_columns=['name', 'price'])
# Access returns a row
item = row_reader['123'] # ['Widget', 19.99]
Database Connection Examples
SQLite
from sqldol import SQLAlchemyStore
# File-based SQLite
store = SQLAlchemyStore('sqlite:///my_database.db', 'my_table')
# In-memory SQLite
store = SQLAlchemyStore('sqlite:///:memory:', 'my_table')
PostgreSQL
store = SQLAlchemyStore(
'postgresql://user:password@localhost:5432/my_db',
'my_table'
)
MySQL
store = SQLAlchemyStore(
'mysql://user:password@localhost/my_db',
'my_table'
)
Working with Complex Data Types
from sqldol import SQLAlchemyStore, SQLAlchemyPersister
from sqlalchemy import JSON
# Store with JSON field
store = SQLAlchemyStore(
uri='sqlite:///example.db',
collection_name='products',
key_fields={'product_id': SQLAlchemyPersister.TYPE_STRING},
data_fields={
'name': SQLAlchemyPersister.TYPE_STRING,
'metadata': JSON # Store complex data as JSON
}
)
# Store complex data
store[{'product_id': 'PROD001'}] = {
'name': 'Smartphone',
'metadata': {
'specs': {'ram': '8GB', 'storage': '128GB'},
'tags': ['electronics', 'mobile'],
'reviews': {'average': 4.5, 'count': 150}
}
}
Tuple-based Operations
import sqlalchemy as sa
from sqldol import SQLAlchemyTupleStore
# Work with tuples for both keys and values
tuple_store = SQLAlchemyTupleStore(
uri='sqlite:///:memory:',
collection_name='coordinates',
key_fields={'x': sa.Integer, 'y': sa.Integer},
data_fields={'label': sa.String, 'value': sa.Float},
)
# (a plain list of names also works; every column then gets ``default_field_type``, a String)
# Keys and values are tuples
tuple_store[(10, 20)] = ('Point A', 42.0)
point_data = tuple_store[(10, 20)] # ('Point A', 42.0)
Overview
sqldol provides a pythonic, dict-like interface to SQL databases using SQLAlchemy under the hood. It allows you to interact with SQL tables as if they were Python dictionaries or lists, making database operations more intuitive and less verbose.
Key Features
- Dict-like interface: Access SQL tables like Python dictionaries
- Multiple store types: Choose from various store implementations based on your needs
- SQLAlchemy integration: Built on top of SQLAlchemy for robust database support
- Flexible data formats: Work with rows as tuples, dictionaries, or raw data
- Simple CRUD operations: Create, read, update, and delete with familiar Python syntax
Advanced Usage
Database Collections
from sqldol import SqlDbReader
# Access multiple tables in a database
db = SqlDbReader.from_uri('sqlite:///my_app.db')
# List all tables
tables = list(db) # ['users', 'products', 'orders']
# Access specific table
users_table = db['users']
all_users = list(users_table) # List of all user rows
Custom Persisters
from sqldol import SQLAlchemyPersister
# Lower-level access for custom operations
persister = SQLAlchemyPersister(
uri='sqlite:///custom.db',
collection_name='events',
key_fields={'event_id': SQLAlchemyPersister.TYPE_STRING},
data_fields={'timestamp': SQLAlchemyPersister.TYPE_STRING, 'data': SQLAlchemyPersister.TYPE_TEXT}
)
# Direct access to the SQLAlchemy query of the table (a free function, not a method:
# a method would be served unchanged by every dol wrapper, in the persister's coordinates)
from sqldol import ops
query_result = ops.query(persister).filter_by(event_id='EVT001').first()
# Under a wrapper (e.g. SQLAlchemyTupleStore) the rows are the persister's ORM rows, not
# the store's tuples: ops.query refuses unless you say so
# ops.query(tuple_store, leaf_coordinates=True)
Microsoft Access files (.mdb, .accdb)
Read an Access file (Jet3/Access 97, Jet4, ACE; any extension) as a Mapping of tables, or copy it into SQLite and use every sqldol store on it. A reading backend is needed, and none is a hard dependency: the mdbtools programs on your PATH (brew install mdbtools, apt install mdbtools; the more complete reader), or pip install "sqldol[access]" (pure-Python access-parser). With both, the first that opens the file is used. sqldol.access.check_requirements() says which you have.
from sqldol import AccessReader, AccessColumns, access_engine, SqlDictReader
db = AccessReader('inventory.mdb') # backend: first that opens it, or backend='mdbtools'
list(db) # ['Items', 'Categories']
AccessColumns(db)['Items'] # ['ID', 'Title', 'Price']
db['Items'][0] # {'ID': 1, 'Title': 'alpha', 'Price': 1.5}
engine = access_engine(db) # in-memory SQLite copy (sqlite_path=... to keep it)
SqlDictReader(engine, 'Items', key_columns='ID')[1]
Values come back the same from both backends: Currency/Numeric as Decimal, Yes/No as bool, GUIDs as '{XXXXXXXX-...}', date/times as 'YYYY-MM-DD HH:MM:SS'. The engine is a snapshot: writes go to the SQLite copy, never back to the Access file.
Testing
The project includes comprehensive tests. To run them:
# Install test dependencies
pip install pytest
# Run tests
pytest sqldol/tests/
Supported Data Types
The constants below are attributes of SQLAlchemyPersister:
TYPE_INTEGER: Integer valuesTYPE_STRING: Variable-length stringsTYPE_TEXT: Long text fieldsTYPE_BOOLEAN: Boolean valuesTYPE_BLOB: Binary data- Plus any SQLAlchemy type (JSON, DateTime, etc.)
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
License
[License information would go here]
Related Projects
- dol - The underlying framework for dict-like interfaces
- SQLAlchemy - The SQL toolkit powering sqldol
Metadata
Release files for sqldol 0.1.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqldol-0.1.17.tar.gz | 89.4 kB | Without provenance | Details for sqldol-0.1.17.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqldol-0.1.17-py3-none-any.whl | Python 3 | none | any | Details for sqldol-0.1.17-py3-none-any.whl |
Total release size: 168.9 kB
Release files / sqldol-0.1.17.tar.gz
| Download URL | sqldol-0.1.17.tar.gz |
|---|---|
| Size | 89.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4202ac65328b4175933f977d8eb5ea341f52d1b2a579082b0d31ce3626ee558
|
|
BLAKE2b-256 checksum How to use checksums |
561d02437c94fd64b3231693e44dc9c52e03933e0e2baa9d26fd0b0236213c31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / sqldol-0.1.17-py3-none-any.whl
| Download URL | sqldol-0.1.17-py3-none-any.whl |
|---|---|
| Size | 79.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fb6a7ada0bfcc631ed30f14457daa1840f93d71e141fcc9590f651fd890f09f4
|
|
BLAKE2b-256 checksum How to use checksums |
b1c027abd863d058fa2235571b57352be94faef84668db05bc85e9ad1fae93d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for sqldol 0.1.17
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.