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 ...
随机推荐
- 小型数据库性能对比(对比了SQLite、FireBird、FastDB、SQLServer2000绿色版、Access、BDB、PostgreSQL)
一.全面对比 SQLite.FireBird.FastDB.SQLServer2000绿色版.Access.BDB.PostgreSQL对比结果如下: 二.国产数据库 三.SQLite 与Postgr ...
- javaee和javase的区别
JavaEE是指Java Enterprise Edition,Java企业版,多用于企业级开发,包括web开发等等.也叫J2EE. JavaSE通常是指Java Standard Edition,J ...
- vivado2017.1和modelsim10.5联合仿真
vivado2017.1和modelsim版本 vivado可在xilinx官网查询匹配的modelsim最低版本,下载modelsim前记得选用合适的版本号,如图 查询地址:https://www. ...
- LG5003 跳舞的线 - 乱拐弯 线性DP
问题描述 LG5003 题解 设 \(mx[i][j][0/1]\)代表当前位置.朝向的最大拐弯数,最小同理. 来源为左边和上边. 坑点:起点可能为#. \(\mathrm{Code}\) #incl ...
- 监控ckeditor内容变化,删除编辑器内图片,ueditor同样适用
let body = document.querySelector("iframe").contentDocument.body; let observer = new Mutat ...
- PHP 高级面试题 - 如果没有 mb 系列函数,如何切割多字节字符串
需求 如果需要将可能含有中文的字符串进行拆分成数组,我们下面以 utf-8 编码为例. 解决方案一 我习惯的方法可能是: mb_internal_encoding("UTF-8") ...
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 6
23.4 API的设计原则和规范 API是服务提供方和使用方之间对接的通道,前面我们设计的一些简单API的例子,基本上比较随意,没有使用任何规范.设想一下,每个平台都可能存在大量的API,如果API ...
- Seven Kinds of Testers - 七种类型的测试
最近读了James大叔的一篇总结Tester类型的文章,获益良多.原文叫做Seven Kinds of Testers(链接:http://www.satisfice.com/blog/archive ...
- PHP TP框架自定义打印函数P
效果如下,有个灰色背景,也不一定是灰色可以改 代码: //传递数据以易于阅读的样式格式化后输出function p($data){ // 定义样式 $str='<pre style=" ...
- PVANET: Deep but Lightweight Neural Networks for Real-time Object Detection
URL: https://arxiv.org/abs/1608.08021 year: 2016 TL;DR PVANet 一个轻量级多物体目标检测架构, 遵循 "less channels ...