Skip to main content

Asyncio strategy for SQLAlchemy.

Project description

PyPI Version Documentation Travis Coverage MIT License

sqlalchemy_aio adds asyncio support to SQLAlchemy core, derived from alchimia.

Getting started

import asyncio

from sqlalchemy_aio import ASYNCIO_STRATEGY

from sqlalchemy import (
    MetaData, Table, Column, Integer, String, create_engine, select)
from sqlalchemy.schema import CreateTable

async def main():
    engine = create_engine(
        'sqlite://', strategy=ASYNCIO_STRATEGY
    )

    metadata = MetaData()
    users = Table('users', metadata,
        Column('id', Integer(), primary_key=True),
        Column('name', String()),
    )

    # Create the table
    await engine.execute(CreateTable(users))

    conn = await engine.connect()

    # Insert some users
    await conn.execute(users.insert().values(name='Jeremy Goodwin'))
    await conn.execute(users.insert().values(name='Natalie Hurley'))
    await conn.execute(users.insert().values(name='Dan Rydell'))
    await conn.execute(users.insert().values(name='Casey McCall'))
    await conn.execute(users.insert().values(name='Dana Whitaker'))

    result = await conn.execute(users.select(users.c.name.startswith('D')))
    d_users = await result.fetchall()

    await conn.close()

    # Print out the users
    for user in d_users:
        print('Username: %s' % user[users.c.name])

    # Supports context async managers
    async with engine.connect() as conn:
        async with conn.begin() as trans:
            r = await conn.execute(select([1]))
            assert await r.fetchone() == (1,)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

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

sqlalchemy_aio-0.10.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_aio-0.10.0-py2.py3-none-any.whl (7.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sqlalchemy_aio-0.10.0.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_aio-0.10.0.tar.gz
Algorithm Hash digest
SHA256 db40bdb14312a1c6a153357a1db89e56220fa406bd608916bafa7f7a9d34ad55
MD5 d8f9d4912209d624c6b94cf13e471c33
BLAKE2b-256 bf6315b1be4f762e343eb51dfd0bc1833243e02e6e031ee59b9b620e8872f247

See more details on using hashes here.

Provenance

File details

Details for the file sqlalchemy_aio-0.10.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_aio-0.10.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b374e64f9b89a153d81574d3693e423788566c7d8e5ca76b4659cba9334b3e12
MD5 af4944c0c489234bfa174d1c5a4a23cf
BLAKE2b-256 998025ca095d711a8fcc73b4313e4b237da054316fca33f45cfc8762e369fe8a

See more details on using hashes here.

Provenance

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