import asyncio
import aiomysql
async def async_ope_mysql():
sql_conn = await aiomysql.connect(host="", user="xx", password="!xx",
db="xx", port=3306)
cur = await sql_conn.cursor()
sql = "select Host, User from user;"
await cur.execute(sql)
u = await cur.fetchall()
print(u)
await cur.close()
sql_conn.close()
async def main():
tasks = [
asyncio.ensure_future(async_ope_mysql()),
asyncio.ensure_future(async_ope_mysql())
]
await asyncio.wait(tasks)
if __name__ == '__main__':
p = asyncio.get_event_loop()
p.run_until_complete(main())
asyncio异步编程之异步mysql
于 2022-03-09 18:30:01 首次发布
这段代码展示了如何在Python中利用aiomysql库进行异步数据库操作。它创建了一个连接,执行SQL查询来获取MySQL user表中的Host和User信息,并打印了所有结果。最后,关闭了游标和数据库连接。
608

被折叠的 条评论
为什么被折叠?



