Skip to main content

Databases + asyncio.

Project description


Databases gives you simple asyncio support for a range of databases. Currently PostgreSQL and MySQL are supported.

$ pip install databases

Declare your tables using SQLAlchemy:

import sqlalchemy

metadata = sqlalchemy.MetaData()

notes = sqlalchemy.Table(
    sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
    sqlalchemy.Column("text", sqlalchemy.String(length=100)),
    sqlalchemy.Column("completed", sqlalchemy.Boolean),

You can now use SQLAlchemy core queries:

from databases import Database

database = Database('postgresql://localhost/example')

# Establish the connection pool
await database.connect()

# Execute
query = notes.insert()
values = {"text": "example1", "completed": True}
await database.execute(query, values)

# Execute many
query = notes.insert()
values = [
    {"text": "example2", "completed": False},
    {"text": "example3", "completed": True},
await database.executemany(query, values)

# Fetch multiple rows
query =
rows = await database.fetchall()

# Fetch single row
query =
row = await database.fetchone()

Transactions are managed by async context blocks:

async with database.transaction():

For a lower-level transaction API:

transaction = await database.transaction()

For strict test isolation you will always want to rollback the test database to a clean state between each test case:

async with database.transaction(force_rollback=True):

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

databases-0.0.2.tar.gz (7.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page