我使用的是aiomysql和MariaDB。我可以创建一个表或选择数据,但是我不能将数据插入到表中。如果使用SELECT使用fetchall(),那么它将显示您刚才插入的内容,但会立即从数据库中删除。
async def test_example(loop):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='test', loop=loop)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("INSERT INTO `tbl`(`id`, `val`) VALUES (37, 'z');")
print(cur.fetchall())
pool.close()
await pool.wait_closed()
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example(loop))为什么?
发布于 2020-08-10 12:18:53
从表名和列名中删除引号。
import aiomysql
import asyncio
async def select(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
r = await cur.fetchone()
print(r)
async def insert(loop, sql, pool):
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(sql)
await conn.commit()
async def main(loop):
pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
user='root', password='',
db='test', loop=loop)
c1 = select(loop=loop, sql='select * from tbl', pool=pool)
c2 = insert(loop=loop, sql="INSERT INTO tbl(id, val) VALUES (37, 'z');", pool=pool)
tasks = [asyncio.ensure_future(c1), asyncio.ensure_future(c2)]
return await asyncio.gather(*tasks)
if __name__ == '__main__':
cur_loop = asyncio.get_event_loop()
cur_loop.run_until_complete(main(cur_loop))发布于 2020-08-10 15:13:34
来自佩普-249规范:
.fetchall()
获取查询结果的所有(剩余)行,将它们返回为序列序列(例如,元组列表)。
由于sql INSERT语句不产生结果集,所以在尝试从数据库服务器获取信息之前,应该尝试使用SELECT语句。
https://stackoverflow.com/questions/63340001
复制相似问题