Async Support for various databases
Project description
AIO-Databases
The package gives you asycio support for a range of databases (SQLite, PostgreSQL, MySQL).
Requirements
- python >= 3.7
Installation
aio-databases should be installed using pip:
$ pip install aio-databases
You have to choose and install the required database drivers with:
# To support SQLite
$ pip install aio-databases[aiosqlite]
# To support MySQL
$ pip install aio-databases[aiomysql]
# To support PostgreSQL (choose one)
$ pip install aio-databases[aiopg]
$ pip install aio-databases[asyncpg]
Usage
- Init a database
from aio_databases import Database
db = Database('sqlite:///:memory:')
- Prepare the database to work
async def my_app_starts():
# Initialize a database's pool
await db.connect()
async def my_app_ends():
# Close pool/connections
await db.disconnect()
# As an alternative users are able to use the database
# as an async context manager
async with db:
await my_main_coroutine()
- Run SQL queries
await db.execute('select $1', '1')
await db.executemany('select $1', '1', '2', '3')
records = await db.fetchall('select (2 * $1) res', 2)
assert records == [(4,)]
record = await db.fetchone('select (2 * $1) res', 2)
assert record == (4,)
assert record['res'] == 4
result = await db.fetchval('select 2 * $1', 2)
assert result == 4
- Use transactions
async with db.transaction() as trans1:
# do some work ...
async with db.transaction() as trans2:
# do some work ...
await trans2.rollback()
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/aio-databases/issues
Contributing
Development of the project happens at: https://github.com/klen/aio-databases
License
Licensed under a MIT License
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
aio-databases-0.0.17.tar.gz
(7.9 kB
view hashes)
Built Distribution
Close
Hashes for aio_databases-0.0.17-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29ecd773775e39acc4b967c28c62d5e0799d31b928166cb0329f7b59d40a3dbc |
|
MD5 | 687a4d2967564467216149ceaf26b372 |
|
BLAKE2b-256 | a681c14e6f365fcf90c584001d56c18aef2eb642628b598d368b954c01a9ccf2 |