Databank
Databank is an easy-to-use Python library for making raw SQL queries in a multi-threaded environment.
No ORM, no frills. Only raw SQL queries and parameter binding. Thread-safe. Built on top of SQLAlchemy.
(The photo was taken by Matthew Ratzloff and is licensed under CC BY-NC-ND 2.0.)
Installation
You can install the latest stable version from PyPI:
$ pip install databank
Adapters not included. Install e.g. psycopg for PostgreSQL:
$ pip install psycopg
For AsyncDatabase, install the asyncio extra (and an async adapter):
$ pip install "databank[asyncio]"
Usage
Connect to the database of your choice:
>>> from databank import Database
>>> db = Database("postgresql+psycopg://user:password@localhost/db", pool_size=2)
The keyword arguments are passed directly to SQLAlchemy's create_engine() function. Depending on the database you connect to, you have options like the size of connection pools.
If you are using
databankin a multi-threaded environment (e.g. in a web application), make sure the pool size is at least the number of worker threads.
Let's create a simple table:
>>> db.execute("CREATE TABLE beatles (id SERIAL PRIMARY KEY, member TEXT NOT NULL);")
You can insert multiple rows at once:
>>> params = [
... {"id": 0, "member": "John"},
... {"id": 1, "member": "Paul"},
... {"id": 2, "member": "George"},
... {"id": 3, "member": "Ringo"}
... ]
>>> db.execute_many("INSERT INTO beatles (id, member) VALUES (:id, :member);", params=params)
Fetch a single row:
>>> db.fetch_one("SELECT * FROM beatles;")
{'id': 0, 'member': 'John'}
But you can also fetch n rows:
>>> db.fetch_many("SELECT * FROM beatles;", n=2)
[{'id': 0, 'member': 'John'}, {'id': 1, 'member': 'Paul'}]
Or all rows:
>>> db.fetch_all("SELECT * FROM beatles;")
[{'id': 0, 'member': 'John'},
{'id': 1, 'member': 'Paul'},
{'id': 2, 'member': 'George'},
{'id': 3, 'member': 'Ringo'}]
If you are using PostgreSQL with jsonb columns, you can use a helper function to serialize the parameter values:
>>> from databank.utils import serialize_params
>>> serialize_params({"member": "Ringo", "song": ["Don't Pass Me By", "Octopus's Garden"]})
{'member': 'Ringo', 'song': '["Don\'t Pass Me By", "Octopus\'s Garden"]'}
To log a query with its parameters, compile_sql() renders them as literals. Use it for logging and debugging only, never to execute the result:
>>> from databank.utils import compile_sql
>>> compile_sql("SELECT * FROM beatles WHERE id = :id;", params={"id": 0})
'SELECT * FROM beatles WHERE id = 0;'
Async
You can also use AsyncDatabase which implements basically the same methods as Database (but with an a prefix):
>>> from databank import AsyncDatabase
>>> db = AsyncDatabase("postgresql+psycopg://user:password@localhost/db", pool_size=2)
>>> await db.afetch_one("SELECT * FROM beatles;")
{'id': 0, 'member': 'John'}
acreate_session() works like create_session(). Nested calls within the same task join the outer transaction, while other tasks (e.g. spawned with asyncio.gather) get their own session.
Query Collection
You can also organize SQL queries in an SQL file and load them into a QueryCollection:
/* @name insert_data */
INSERT INTO beatles (id, member) VALUES (:id, :member);
/* @name select_all_data */
SELECT * FROM beatles;
This idea is borrowed from PgTyped
A query must start with a header comment with the name of the query and ends at the next header, so blank lines and comments inside a query are fine. Only comments are allowed before the first header. Query names must be unique, otherwise an InvalidQueryError is raised. You can parse that file and load the queries into a QueryCollection:
>>> from databank import QueryCollection
>>> queries = QueryCollection.from_file("queries.sql")
and access the queries like in a dictionary:
>>> queries["insert_data"]
'INSERT INTO beatles (id, member) VALUES (:id, :member);'
>>> queries["select_all_data"]
'SELECT * FROM beatles;'
Metadata
Release files for databank 1.3.0
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 |
|---|---|---|---|---|
| databank-1.3.0.tar.gz | 33.5 kB | Without provenance | Details for databank-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| databank-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for databank-1.3.0-py3-none-any.whl |
Total release size: 42.3 kB
Release files / databank-1.3.0.tar.gz
| Download URL | databank-1.3.0.tar.gz |
|---|---|
| Size | 33.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4749ea768bfcfe4a3d3ccaa1e2a6aa55d0da520eef40d892049f00d373d80998
|
|
BLAKE2b-256 checksum How to use checksums |
9f886f27955a3e3df1abb7353bf47d4d200c2d0cbe95e46f422c3ba0033cfa1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / databank-1.3.0-py3-none-any.whl
| Download URL | databank-1.3.0-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a70be86dfb8b2e8c4cefc659ad1b41abc17cb611dec9c8ad68d481097679685
|
|
BLAKE2b-256 checksum How to use checksums |
64000f58b2800980096e35c33dff9bb15d8acc5099d8e559a672b136a46735fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for databank 1.3.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.
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.