python异步操作MySQL(aiomysql)
来源:互联网 发布:苗勒管永存综合征 知乎 编辑:程序博客网 时间:2024/05/18 19:20
安装aiomysql
依赖
- Python3.4+
- asyncio
- PyMySQL
安装
pip install aiomysql
应用
基本的异步连接connection
import asynciofrom aiomysql import create_poolloop = 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 asyncioimport aiomysqlasync 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 asyncioimport sqlalchemy as safrom aiomysql.sa import create_enginemetadata = 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))
阅读全文
0 0
- python异步操作MySQL(aiomysql)
- Python数据库操作(MySQL)
- python motor异步mongodb操作
- [python]Python操作MySQL
- aiomysql的官方文档
- python操作MySQL数据库(二)
- python操作mysql数据库(常用函数)
- python 操作mysql数据库(简单/基础)
- Python开发(一)操作Mysql数据库
- Python之操作MySQL数据库(二)
- python操作mysql数据库(高级教程)
- Python的mysql操作(安装、链接)
- Python的mysql操作(事务、游标)
- python操作mysql
- Python操作MySQL
- Python操作Mysql数据库
- python操作MySQL
- python 操作mysql
- spring源码-事务
- Spring(一) 概述
- Tensorflow 学习笔记 (1)官方文档学习—深入mnist
- Python3.5——运算符
- 像企业账号一样分发安装应用
- python异步操作MySQL(aiomysql)
- Python3读写csv文件
- core java chapter one An introduction to java
- [python]format的使用
- 微信电脑端-退出的群、删除的人,仍然可以查找或看到问题的解决方法
- 常用框架总结
- java生成-excel-并导出文件
- javaScript在页面中加载的先后顺序
- 一般论文投稿技巧