aiomysql
aiomysql:
import aiomysql
import asyncio
async def aiomysql_test():
loop = asyncio.get_event_loop()
# 第一种连接方法
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306, user='root', password='root', db='cfda', loop=loop,
charset='utf8', autocommit=True) # autocommit=True 不用在写pool.commit 每次默认自动提交
async with pool.acquire() as conn:
async with conn.cursor() as cursor:
insert_sql = 'insert into async_test_async(title) values("{}")'.format(title)
print('insert_sql:', insert_sql)
# 插入数据
await cursor.execute("insert into async_test_async(title) values('{}')".format(title))
# 查询数据
await cursor.execute("select * from async_test_async")
data = await cursor.fetchall()
print("data:", data)
# 更新数据
await cursor.execute("update async_test_async set title='{}' where id={}".format('update', 10168))
# 删除数据
await cursor.execute("delete from async_test_async where id={}".format(10174))
# 第二种连接方法
pool = await aiomysql.connect(host='127.0.0.1', port=3306, user='root', password='root', db='cfda', loop=loop,
charset='utf8', autocommit=True)
cursor = await pool.cursor()
insert_sql = 'insert into async_test_async(title) values("{}")'.format(title)
print('insert_sql:', insert_sql)
# 插入数据
await cursor.execute("insert into async_test_async(title) values('{}')".format(title))
# 查询数据
await cursor.execute("select * from async_test_async")
data = await cursor.fetchall()
print("data:", data)
# 更新数据
await cursor.execute("update async_test_async set title='{}' where id={}".format('update', 10168))
# 删除数据
await cursor.execute("delete from async_test_async where id={}".format(10174))
cursor.close()
pool.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(aiomysql_test())
aiomysql的更多相关文章
- python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- 2.aiomysql实现对数据库异步读取
有一个库叫做aiomysql,这是一个基于asyncio和pymysql的库.至于为什么可以在tornado中使用,是因为高版本tornado的底层使用了asyncio. import asyncio ...
- aiomysql inserting operation failed !
emotions: those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem ma ...
- aiomysql实现对数据库异步读取
有一个库叫做aiomysql,这是一个基于asyncio和pymysql的库.至于为什么可以在tornado中使用,是因为高版本tornado的底层使用了asyncio. import asyncio ...
- python学习6 web开发
wsgi自带,用语构建简单服务器 例子 from wsgiref.simple_server import make_server def index(env, res): res('200 ok', ...
- [python] ORM 第一次注释
不懂的东西还太多,就当是自己监督自己吧 #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Michael Liao' impor ...
- 浅谈python web框架中的orm设计
看了一下廖雪峰的那个web框架,其实就是封装了web.py,请求使用异步并将aiomysql做为MySQL数据库提供了异步IO的驱动,前端部分则整合了jinja.其中最难的应该是orm部分了. 下面是 ...
- Python3 ORM hacking
#!/usr/bin/env python3 # -*- coding: utf- -*- # # Python3 ORM hacking # 说明: # 之前分析了一个Python2 ORM的源代码 ...
- Python-Blog1-搭建开发环境
注:本系列是根据廖雪峰python实战过程,详情可见(https://www.liaoxuefeng.com/) 环境准备 Python 版本:Python 3.X,查看python版本python ...
随机推荐
- go语言设计模式之memento
memento.go package memento import ( "fmt" ) type State struct { Description string } type ...
- goroutine并发之callback回调
玩玩 package main import ( "fmt" "strings" "sync" ) var wait sync.WaitGr ...
- wepy安装后提示Cannot read property 'addDeps'
最近准备做一个微信小程序,以前一直用的小程序原始api做,但是这次准备用一个框架来做练习,当然在做之前需要比较一下现在小程序框架的优缺点. 经过认真挑选,选定wepy,Taro,uni-app,mpv ...
- AutoCAD配置的Heidi驱动程序未加载
电脑安装的软件越来越多,有的软件也就偶尔使用一下下,于是就找了一个绿化版的AutoCAD,挺好的,可启动时弹出"配置的Heidi驱动程序未加载.切换到默认软件驱动程序". 对于上述 ...
- JavaScript中如何判断数组类型
前言 JavaScript中关于数组的判定问题,一直都是一个必须要掌握的点,那么,运用知识,如何判断一个类型是数组,就需要有对JavaScript使用有着深入的了解. 判断方法 一.Array.isA ...
- 惊!Python能够检测动态的物体颜色!
本篇文章将通过图片对比的方法检查视频中的动态物体,并将其中会动的物体定位用cv2矩形框圈出来.本次项目可用于树莓派或者单片机追踪做一些思路参考.寻找动态物体也可以用来监控是否有人进入房间等等场所的监控 ...
- 【CF933E】A Preponderant Reunion(动态规划)
[CF933E]A Preponderant Reunion(动态规划) 题面 CF 洛谷 题解 直接做很不好搞,我们把条件放宽,我们每次可以选择两个相邻的非零数让他们减少任意值,甚至可以减成负数(虽 ...
- oracle学习笔记(十一) 高级查询
高级查询 分组查询 select * from student [where ] [having ] --二次限定 [order by] --asc升序 desc降序 默认升序 查看EMPLOYEE表 ...
- Python切片中的误区与高级用法
众所周知,我们可以通过索引值(或称下标)来查找序列类型(如字符串.列表.元组...)中的单个元素,那么,如果要获取一个索引区间的元素该怎么办呢? 切片(slice)就是一种截取索引片段的技术,借助切片 ...
- Pi Network有梦想是好的,最新消息和下载注册流程。
(注册流程最下面) ---------------------------------------------------------------------------------- 今日更新(10 ...