zoukankan      html  css  js  c++  java
  • aiomysql inserting operation failed !

    emotions:

    those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem make me trouble more times.The problem is i can't insert data into my database table using aiomysql's cursor.execute(**) method. Fortunately,i sovled it after refering aiomysq's manual.

    plateform:

    1.ubuntu14.04
    2.python3.5

    error code:

    import aiomysql 
    import asyncio
    
    loop=asyncio.get_event_loop()
    @asyncio.coroutine
    def insert(loop):
            pl=yield from aiomysql.create_pool(host='127.0.0.1',user='root',password='xxxxx',db='blog',loop=loop,port=3306)
            with (yield from pl) as con:
                    cursor=yield from con.cursor()
                    yield from cursor.execute('insert into users(`user_name`,`email`,`password`) values("coder","1xxxxxb@gmail.com","FHEUIE@#@@##JNDJA")')
                    yield from cursor.close()
                    con.commit()
            pl.close()
            yield from pl.wait_closed()
    
    loop.run_until_complete(insert(loop))
    loop.run_forever()
    

    I run the above code many times,but there is still no data in my database table.

    solution:

    After refering aiomysql's manual,I found 'aiomysql.commit' method is coroutine,that's a critical point.just rewrite 'con.commit()' to 'yield from con.commit()',the problem is sovled!

    correct code:

    import aiomysql 
    import asyncio
    
    loop=asyncio.get_event_loop()
    @asyncio.coroutine
    def insert(loop):
            pl=yield from aiomysql.create_pool(host='127.0.0.1',user='root',password='xxxxx',db='blog',loop=loop,port=3306)
            with (yield from pl) as con:
                    cursor=yield from con.cursor()
                    yield from cursor.execute('insert into users(`user_name`,`email`,`password`) values("coder","1xxxxxb@gmail.com","FHEUIE@#@@##JNDJA")')
                    yield from cursor.close()
                    yield from con.commit()
            pl.close()
            yield from pl.wait_closed()
    
    loop.run_until_complete(insert(loop))
    loop.run_forever()
    
  • 相关阅读:
    k8s学习笔记之五:Pod资源清单spec字段常用字段及含义
    k8s学习笔记之四:资源清单定义入门
    k8s学习笔记之三:k8s快速入门
    k8s学习笔记之一:kubernetes简介
    k8s学习笔记之二:使用kubeadm安装k8s集群
    centos7安装elasticsearch6.3.x集群并破解安装x-pack
    Centos6搭建sftp服务器
    底层互害模式,深契民心
    你不视我为子女,我凭什么视你为父母
    nodejs的桌面应用(electron)
  • 原文地址:https://www.cnblogs.com/whereareyoufrom/p/5269010.html
Copyright © 2011-2022 走看看