pip install aiomysql
python
import aiomysql
async def create_connection():
conn = await aiomysql.connect(host='localhost', port=3306,
user='root', password='password',
db='database', loop=asyncio.get_event_loop())
python
async def execute_query():
async with conn.cursor() as cursor:
await cursor.execute('SELECT * FROM table')
result = await cursor.fetchone()
print(result)
python
async def close_connection():
conn.close()
await conn.wait_closed()
python
import asyncio
async def main():
conn = await create_connection()
await execute_query()
await close_connection()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())