如何使用aiomysql
使用示例(插入)
# -*- coding: utf-8 -*-
# Author: 桑葚ICE
# Email: 152516cc@gmail.com
# Blog: iicey.github.io
# JueJin: juejin.im/user/5c64dce8e51d45013c40742c
import asyncio
import aiomysql
async def main():
loop = asyncio.get_event_loop()
conn = await aiomysql.connect(host='127.0.0.1',
port=3306,
user='root',
password='',
db='aio_test',
loop=loop)
sql = "INSERT INTO table_name(`field1`, `field2`) VALUES (%s, %s);"
values = ("value1", "value2")
async with conn.cursor() as cur:
await cur.execute(query=sql, args=values)
await conn.commit()
conn.close()
if __name__ == '__main__':
asyncio.run(main())
其它操作
请参照pymysql改写,传送门