异步读写mysql依赖pymysql (asyncio/ aiomysql)

代码

`import asyncio
import aiomysql

settings = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "123456",
"db": "test",
}

async def main():
pool = await aiomysql.create_pool(
host=settings["host"],
port=settings["port"],
user=settings["user"],
password=settings["password"],
db=settings["db"],
)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT id,user_name FROM user")
# print(cur.description)
data = await cur.fetchone()
print(data)
pool.close()
await pool.wait_closed()

if name == 'main':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())`

posted @ 2025-10-06 09:26  陈绪水  阅读(11)  评论(0)    收藏  举报