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. 5. IDEA使用

    一.IDEA项目结构 二.创建项目 三.快捷键 Alt + Enter // 导包,自动修正代码 ctrl + Y // 删除光标所在行 ctrl + D // 复制光标所在行,插在光标位置下面 ct ...

  2. 在 VS 中查看所有线程

    查看当前所有线程 在程序中打断点,然后启动调试(注意:线程窗口需要启动调试才可以看得到)点击 Debug > Windows > Threads 线程窗口如下:

  3. cf 【并查集】

    http://codeforces.com/contest/1245/problem/D 题意就是:你需要让所有城市都有电,你看也在该城市建电站使他有电,同时你可以链接他与其他城市,使之有电 解决: ...

  4. acwing 17. 从尾到头打印链表

    题目地址 https://www.acwing.com/problem/content/description/18/ 来源:剑指Offer 输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值. ...

  5. 用vbs和ADSI管理Windows账户

    ADSI (Active Directory Services Interface)是Microsoft新推出的一项技术,它统一了许多底层服务的编程接口,程序员可以使用一致的对象技术来访问这些底层服务 ...

  6. 【2019.8.20 NOIP模拟赛 T3】小X的图(history)(可持久化并查集)

    可持久化并查集 显然是可持久化并查集裸题吧... 就是题面长得有点恶心,被闪指导狂喷. 对于\(K\)操作,直接\(O(1)\)赋值修改. 对于\(R\)操作,并查集上直接连边. 对于\(T\)操作, ...

  7. EF Core 3.0 Preview 9 的2个小坑

    之前我们的数据库服务器使用的是 SQL Server 2008 R2 ,由于从 EF Core 3.0 Preview 6 开始不支持 UseRowNumberForPaging ,只能停留在 EF ...

  8. python request获取ip、获取登录设备

    from flask import request 获取ip request.remote_addr 获取登录设备 request.user_agent.string

  9. IT兄弟连 Java语法教程 Java语法基础 经典面试题

    1.Java语言中有几种基本类型?分别是什么?请详细说明每种类型的范围以及所占的空间大小? Java语言中有8中基本类型,分别是代表整形的byte.short.int和long,代表浮点型的float ...

  10. Linux目录结构-下部

    第1章 /etc目录 1.1 /etc/inittab 1.1.1 查看当前系统的运行级别 [root@nfsnobody ~]# runlevel N 3##查看系统当前运行级别 后面的数字表示当前 ...