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. いくnotepad++

    再见!Notepad++,好走不送! 1No zuo No Die 上周就发现Notepad++开发者在作妖,新版本放了个啥恶心的标注上来,本来想直接发文说一说,后来想想是不是这样又给它做了宣传,就决 ...

  2. springmvc的文件上传和JWT图形验证码

    相关pom依赖 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commo ...

  3. React 以两种形式去创建组件 类或者函数(二)

    08==>创建组件以 1类的形式 或者以 2函数的形式 09==>使用组件 在src下创建components文件夹 是放组件的 CompType.js 组件 组件开头大写(重要) Com ...

  4. JS---DOM---为元素解除绑定事件

    解除绑定事件: 1.解绑事件 对象 .on 事件名字=事件处理函数--->绑定事件. 对象 .on 事件名字 = null . 注意:用什么方式绑定事件,就应该用对应的方式解除绑定事件. //1 ...

  5. C语言解决汉诺塔问题!

    很难受,看了很多资料才明白..... 对这个问题分析,发现思路如下:有n个黄金盘,要先把n-1个弄到B柱上,再把第n个弄到C柱上,然后把n-1个借助A柱弄到C柱上. 实现的函数如下: void f(i ...

  6. 【洛谷5335】[THUSC2016] 补退选(指针实现Trie)

    点此看题面 大致题意: 三种操作:加入一个字符串,删除一个字符串,求最早什么时候以某个字符串为前缀的字符串个数超过给定值. \(Trie\) 这道题显然是\(Trie\)的暴力裸题. 考虑我们对于\( ...

  7. 报错:Something is already running on port 8000.

    在用react框架的时候,用cnpm run dev命令执行项目时,有时会出现这种错误, 这是因为你之前执行过该命令,但是没关闭,解决办法是打开任务管理器, 在进程中找到node.exe,右键关闭这个 ...

  8. 基于Django的Rest Framework框架的解析器

    本文目录 一 解析器的作用 二 全局使用解析器 三 局部使用解析器 四 源码分析 回到目录 一 解析器的作用 根据请求头 content-type 选择对应的解析器对请求体内容进行处理. 有appli ...

  9. 华为mate10 pro内置浏览器出现的令人头疼的样式兼容问题

    问题描述:   下图红色框区域内容在华为mate10 pro(以下简称mate10)内置浏览器中整体向左偏移,没有居中,其它手机浏览器都无该问题,如下图 问题分析   经过一番追根溯源,我发现是 bo ...

  10. WPF 修改Webbrowser的IE版本小程序(32位)

    偶尔用Winform的Webbrowser,但是ie版本太低. 手改改注册表了太慢了. 弄个了程序,修改的代码时网上的,自己就是写了个界面. 支持IE11. XAML页面代码 <Window.R ...