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的更多相关文章

  1. python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  2. 2.aiomysql实现对数据库异步读取

    有一个库叫做aiomysql,这是一个基于asyncio和pymysql的库.至于为什么可以在tornado中使用,是因为高版本tornado的底层使用了asyncio. import asyncio ...

  3. aiomysql inserting operation failed !

    emotions: those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem ma ...

  4. aiomysql实现对数据库异步读取

    有一个库叫做aiomysql,这是一个基于asyncio和pymysql的库.至于为什么可以在tornado中使用,是因为高版本tornado的底层使用了asyncio. import asyncio ...

  5. python学习6 web开发

    wsgi自带,用语构建简单服务器 例子 from wsgiref.simple_server import make_server def index(env, res): res('200 ok', ...

  6. [python] ORM 第一次注释

    不懂的东西还太多,就当是自己监督自己吧 #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Michael Liao' impor ...

  7. 浅谈python web框架中的orm设计

    看了一下廖雪峰的那个web框架,其实就是封装了web.py,请求使用异步并将aiomysql做为MySQL数据库提供了异步IO的驱动,前端部分则整合了jinja.其中最难的应该是orm部分了. 下面是 ...

  8. Python3 ORM hacking

    #!/usr/bin/env python3 # -*- coding: utf- -*- # # Python3 ORM hacking # 说明: # 之前分析了一个Python2 ORM的源代码 ...

  9. Python-Blog1-搭建开发环境

    注:本系列是根据廖雪峰python实战过程,详情可见(https://www.liaoxuefeng.com/) 环境准备 Python 版本:Python 3.X,查看python版本python ...

随机推荐

  1. acwing 652. 切蛋糕

    题目地址 今天是小Z的生日,同学们为他带来了一块蛋糕. 这块蛋糕是一个长方体,被用不同色彩分成了N个相同的小块,每小块都有对应的幸运值. 小Z作为寿星,自然希望吃到的第一块蛋糕的幸运值总和最大,但小Z ...

  2. leetcode 1041. 困于环中的机器人

    题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...

  3. npm简单实用

    npm包管理工具 npm可以理解为前端的maven,一个包的管理工具 1. 查看npm和node版本 node -v npm -v 2. 初始化项目 npm init 默认配置初始化项目 npm in ...

  4. .NET Core 3.1 Preview 1 发布

    今天,我们正式发布 .NET Core 3.1 Preview 1..NET Core 3.1将是一个小版本,着重于Blazor和Windows桌面开发的功能改进,同时这也是.NET Core 3.0 ...

  5. PHP面试题大全(值得收藏)

    PHP进阶.面试:文档.视频资源点击免费获取 一 .PHP基础部分 1.PHP语言的一大优势是跨平台,什么是跨平台? PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操 ...

  6. numpy的一点学习

    1.Numpy模块 NumPy是Python中的一个运算速度非常快的一个数学库,它非常重视数组.它允许你在Python中进行向量和矩阵计算,并且由于许多底层函数实际上是用C编写的,因此你可以体验在原生 ...

  7. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  8. 使用HTMLTestRunner模块更优美地展示接口测试报告

    优化版本的HTMLTestRunner模块,从我的百度网盘获取: 链接:https://pan.baidu.com/s/1f8eLpX5qBrpJsVlXKjquRQ 提取码:qqlu 测试报告展示: ...

  9. 洛谷 P1351 (枚举)

    ### 洛谷P1351 题目链接 ### 题目大意: 给你 n 个节点, n-1 条边的无向联通图.若定义(u,v)表示 u 与 v 点的最短距离,如果 (u,v)值为 2 ,则这两个点的点权之积(即 ...

  10. Python巧用法

    #for 与 else 搭配使用(使用break跳过else) a=[1,2,3,4,5] for i in a: print(i) else: print(i, 'I am else!') for ...