Python SQLite combined with syntax compared to asyncpg progject.
Project description
PostgresLite
Python SQLite library built around asyncpg-style syntax.
No longer do you need to wrestle with raw SQLite. PostgresLite gives you a cleaner, asyncpg-inspired API with everything you'd expect out of the box:
- Automatic type conversion for
DATE,DATETIME,TIMESTAMP, andJSONcolumns - Transaction support via context managers
- Schema syncing and dumping
- Both sync and async connections, using the same API
Installing
You need Python >=3.11 to use this library.
Install by using pip install postgreslite in the terminal.
Quick example
Sync
from postgreslite import PostgresLite
pool = PostgresLite("./hello_world.db").connect()
pool.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL)")
pool.execute("INSERT INTO users (name) VALUES (?) ON CONFLICT DO NOTHING", "AlexFlipnote")
data = pool.fetch("SELECT * FROM users")
print(data) # [{'id': 1, 'name': 'AlexFlipnote'}]
pool.close()
Async
import asyncio
from postgreslite import PostgresLite
async def main():
pool = PostgresLite("./hello_world.db").connect_async()
data = await pool.fetch("SELECT * FROM users")
await pool.close()
asyncio.run(main())
Both connection types support the same API: execute, fetch, fetchrow, fetchval, executemany.
asyncpg-style placeholders ($1, $2, …) are also accepted and converted automatically.
Type handling
Columns declared as DATE, DATETIME, TIMESTAMP, or JSON are automatically converted on read:
| Column type | Python type |
|---|---|
DATE |
datetime.date |
DATETIME |
datetime.datetime |
TIMESTAMP |
datetime.datetime (UTC) |
JSON |
dict / list |
Transactions
with pool.transaction():
pool.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")
pool.execute("UPDATE accounts SET balance = balance + 100 WHERE id = 2")
Raises on error and rolls back automatically. Async pools use async with pool.transaction().
Schema utilities
db = PostgresLite("./hello_world.db")
# Dump current schema to stdout, a file, or a folder of per-table files
db.dump_schema()
db.dump_schema(output="schema.sql")
db.dump_schema(output="schema/")
# Sync a schema definition (SQL string, .sql file, or folder) to the live database
result = db.sync_schema("schema/")
sync_schema adds new tables and missing columns non-destructively by default (safe_mode=True).
Introspection
pool.tables() # ['users', 'posts']
pool.table_exists("users") # True
pool.table_columns("users") # [<TableColumn name='id' ...>, ...]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file postgreslite-1.0.0.tar.gz.
File metadata
- Download URL: postgreslite-1.0.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e1b03fcac0b4e9199627e17bfccb897bcf47b3a9ab760825a0164c8caea1750
|
|
| MD5 |
c163ca070ee8e84368519884bf39f623
|
|
| BLAKE2b-256 |
1fb10cc67ba139ced37927a6b661930547be30ca35f8d58e72f859d0f5a08e77
|
File details
Details for the file postgreslite-1.0.0-py3-none-any.whl.
File metadata
- Download URL: postgreslite-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9a181e193005b9f10cf50221d0ad96c06fd1760262a044872d865e4165cfb52
|
|
| MD5 |
e9b2eee94419ab0f303c7869af59867f
|
|
| BLAKE2b-256 |
dd79b3ec471ef95a15f4b07facbb22b3fb34a8fcc1130b73cf3fe26a10d7fc75
|