Welcome to aiomysql’s documentation!¶
aiomysql is a library for accessing a MySQL database from the asyncio (PEP-3156/tulip) framework. It depends and reuses most parts of PyMySQL . aiomysql tries to be like awesome aiopg library and preserve same api, look and feel.
Internally aiomysql is copy of PyMySQL, underlying io calls switched
to async, basically await
and async def coroutine
added in
proper places. sqlalchemy support ported from aiopg.
Features¶
Implements asyncio DBAPI like interface for MySQL. It includes Connection, Cursor and Pool objects.
Implements optional support for charming sqlalchemy functional sql layer.
Basics¶
aiomysql based on PyMySQL , and provides same api, you just need
to use await conn.f()
instead of just call conn.f()
for
every method.
Properties are unchanged, so conn.prop
is correct as well as
conn.prop = val
.
See example:
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
async def test_example():
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='', db='mysql',
loop=loop)
cur = await conn.cursor()
await cur.execute("SELECT Host,User FROM user")
print(cur.description)
r = await cur.fetchall()
print(r)
await cur.close()
conn.close()
loop.run_until_complete(test_example())
Installation¶
pip3 install aiomysql
Also you probably want to use aiomysql.sa
.
aiomysql.sa
module is optional and requires
sqlalchemy. You can install sqlalchemy by running:
pip3 install sqlalchemy
Source code¶
The project is hosted on GitHub
Please feel free to file an issue on bug tracker if you have found a bug or have some suggestion for library improvement.
The library uses GitHub Actions for Continuous Integration and Codecov for coverage reports.
Dependencies¶
Python 3.7+
aiomysql.sa requires sqlalchemy.