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 ...
随机推荐
- 007.MongoDB特殊成员
一 MongoDB成员 1.1 常见特殊member Secondary存在一些特殊的成员类型: Priority 0 #不能升为主,可以用于多数据中心场景 Hidden #对客户端来说是不可见的,一 ...
- hibernate关联关系一对多
1. 什么是关联(association) 1.1 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性 首先建2个实体类 Order.java package com ...
- 05_javaSE面试题:成员变量和局部变量
题目 /** * 类变量:static修饰的 * 实例变量:不是static修饰的 * * 局部变量:栈 * 实例变量:堆 * 类变量:方法区 * @author kevin * @date 2019 ...
- plsql查询数据显示为乱码解决方案
1.首先安装plsql之后连接数据库,发现使用sql查询出来的中文数据是??,即乱码.原因,因为数据库的编码与本地的编码不一致,plsql默认加载的是本机win10的编码. 2.解决办法: 参数如下: ...
- angular6 升级到 angular7+ 最新Ng-zorro
angular7 出来有一段时间了,然后我们项目一直用的是angular6, 看到一直再用的Ng-Zorro 更新版本了,然后就觉得把目前的项目也升级一下把. 目前我本地cli版本是6.0.8我要把他 ...
- C语言程序设计100例之(13):最大子段和
例13 最大子段和 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大.例如在序列2,-4,3,-1,2,-4,3中,最大的子段和为4,该子段为3,-1,2. 输入格式 第一 ...
- 洛谷P5322 (BJOI 2019) DP
### 题目链接 ### 分析: 1.用 vector<int> v[i] 来存 i 城堡, s 个对手所安排的士兵数量. 2.设 dp[i][j] 表示 i 城堡前,在当前最大派兵量为 ...
- C++入门到理解阶段二基础篇(2)——C++注释、变量、常量、关键字、标识符
目录 1.注释 注释作用 注释的方式 2.变量 变量基本知识 定义变量 3.常量 常量基本知识 整数常量 浮点常量 布尔常量 字符常量 字符串常量 常量定义 使用 #define 预处理器. 使用 c ...
- hive引擎的选择:tez和spark
背景 mr引擎在hive 2中将被弃用.官方推荐使用tez或spark等引擎. 选择 tez 使用有向无环图.内存式计算. spark 可以同时作为批式和流式的处理引擎,减少学习成本. 问题& ...
- 使用pytorch时所遇到的问题总结
使用pytorch时所遇到的问题总结 1.ubuntu vscode切换虚拟环境 在ubuntu系统上,配置工作区文件夹所使用的虚拟环境.之前笔者误以为只需要在vscode内置的终端上将虚拟环境切换过 ...