Python异步操作MySQL示例【使用aiomysql】
作者:Moxiao__墨箫 时间:2024-01-16 20:22:07
本文实例讲述了Python异步操作MySQL。分享给大家供大家参考,具体如下:
安装aiomysql
依赖
Python3.4+
asyncio
PyMySQL
安装
pip install aiomysql
应用
基本的异步连接connection
import asyncio
from aiomysql import create_pool
loop = asyncio.get_event_loop()
async def go():
async with create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='mysql', loop=loop) as pool:
async with pool.get() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 42;")
value = await cur.fetchone()
print(value)
loop.run_until_complete(go())
异步的连接池 pool
import asyncio
import aiomysql
async def test_example(loop):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='mysql', loop=loop)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 42;")
print(cur.description)
(r,) = await cur.fetchone()
assert r == 42
pool.close()
await pool.wait_closed()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))
对象关系映射SQLAlchemy - Object Relationship Mapping
可以随意定义表结构,轻松调用查询、插入等操作方法。
import asyncio
import sqlalchemy as sa
from aiomysql.sa import create_engine
metadata = sa.MetaData()
tbl = sa.Table('tbl', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('val', sa.String(255)))
async def go(loop):
engine = await create_engine(user='root', db='test_pymysql',
host='127.0.0.1', password='', loop=loop)
async with engine.acquire() as conn:
await conn.execute(tbl.insert().values(val='abc'))
await conn.execute(tbl.insert().values(val='xyz'))
async for row in conn.execute(tbl.select()):
print(row.id, row.val)
engine.close()
await engine.wait_closed()
loop = asyncio.get_event_loop()
loop.run_until_complete(go(loop))
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/ydyang1126/article/details/78226701
标签:Python,异步操作,MySQL,aiomysql
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
GoFrame错误处理常用方法及错误码使用示例
2024-04-25 15:30:35
Python+Pygame实现神庙逃亡游戏
2022-06-12 16:26:28
![](https://img.aspxhome.com/file/2023/6/79506_0s.jpg)
Pycharm远程调试和MySQL数据库授权问题
2024-01-14 10:56:00
![](https://img.aspxhome.com/file/2023/9/127279_0s.png)
MySQL外键设置的方法实例
2024-01-19 14:10:42
![](https://img.aspxhome.com/file/2023/9/79029_0s.png)
Pytorch中torch.nn.Softmax的dim参数用法说明
2023-08-28 04:20:10
![](https://img.aspxhome.com/file/2023/4/131174_0s.png)
Gradio机器学习模型快速部署工具应用分享前篇
2023-07-23 12:10:45
![](https://img.aspxhome.com/file/2023/5/62845_0s.jpg)
Python实现图片裁剪的两种方式(Pillow和OpenCV)
2022-07-08 12:42:47
![](https://img.aspxhome.com/file/2023/8/133358_0s.jpg)
pycharm第三方库安装失败的问题及解决经验分享
2023-07-11 01:50:45
![](https://img.aspxhome.com/file/2023/5/107035_0s.png)
python3对拉勾数据进行可视化分析的方法详解
2022-04-04 03:02:32
![](https://img.aspxhome.com/file/2023/1/79761_0s.png)
python的scipy实现插值的示例代码
2022-11-22 15:30:53
![](https://img.aspxhome.com/file/2023/4/93804_0s.png)
InnoDb 体系架构和特性详解 (Innodb存储引擎读书笔记总结)
2024-01-21 14:21:03
Python制作一个仿QQ办公版的图形登录界面
2021-06-23 20:08:49
![](https://img.aspxhome.com/file/2023/1/101611_0s.png)
python3使用diagrams绘制架构图的步骤
2022-08-27 12:42:34
![](https://img.aspxhome.com/file/2023/8/87458_0s.png)
PyQt5中向单元格添加控件的方法示例
2023-10-20 05:08:48
![](https://img.aspxhome.com/file/2023/6/131396_0s.gif)
oracle 字符串转成行
2009-06-19 17:38:00
Jupyter notebook 远程配置及SSL加密教程
2021-06-24 07:15:06
![](https://img.aspxhome.com/file/2023/5/115175_0s.jpg)
Mysql 取字段值逗号第一个数据的查询语句
2024-01-23 14:29:09
Python中的高级数据结构详解
2022-02-20 04:01:09
![](https://img.aspxhome.com/file/2023/4/72844_0s.jpg)
python3.6 如何将list存入txt后再读出list的方法
2022-03-03 07:30:26
![](https://img.aspxhome.com/file/2023/6/115516_0s.png)
ASP网站程序自动升级实现的方法
2008-10-15 14:28:00