Asyncio strategy for SQLAlchemy.
Project description
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
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
sqlalchemy_aio-0.11.0.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemy_aio-0.11.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c2a46504c999b4ae5751c90a86aa78cee8cc216f9d432264fb850ffe2d78965 |
|
MD5 | 9107f56a3ebfe7dcc30c57491d074fc9 |
|
BLAKE2b-256 | 8d5498f52442306fdfdb0d8605e869f8ddd71e48167fd7f40cfcda7dc8f0d2e6 |