5

当我使用mysql_poolfromaiomysql更新一条数据时,第一次与第二次相同。

class Test(object):
    async def _pool(self):
        self.pool = await aiomysql.create_ç(**mysql_options)

    async def get_one(self, sql, param=None):
        await self.cur.execute(sql, param)
        result = await self.cur.fetchone()
        return result

    async def get(self):
        self.conn = await self.pool.acquire()
        self.cur = await self.conn.cursor(DictCursor)
        sql = '''select policy from tb_user where id = 2;'''
        res = await self.get_one(sql)
        print(res)
        await self.cur.close()
        await self.pool.release(self.conn)

    @staticmethod
    def update():
        import pymysql
        coon = pymysql.connect(host='127.0.0.1',
                               port=3306,
                               user=mysql_options['user'],
                               autocommit=True,
                               password=mysql_options['password'],
                               database=mysql_options['db'])
        cursor = coon.cursor()
        sql = '''update tb_user set policy = 9 where id = 2;'''
        cursor.execute(sql)
        sql = '''select policy from tb_user where id = 2;'''
        cursor.execute(sql)
        data = cursor.fetchone()
        print(data)
    async def run(self):
        await self._pool()
        await self.get()
        self.update()
        await self.get()


if __name__ == '__main__':
    test = Test()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test.run())

版本

  • 蟒蛇 3.7.0
  • aiomysql 0.0.20

结果:

在此处输入图像描述

4

1 回答 1

0

你需要承诺。await conn.commit()即使只是SELECT查询也能运行。

于 2021-10-22T10:21:27.103 回答