aiomysql数据库连接池不能正确读取数据?

发布于 2022-09-12 02:35:13 字数 1761 浏览 13 评论 0

问题描述

用数据库连接池查询数据的时候,如果手动更改数据库数据,再次查询时的数据为之前的未更改的数据

问题出现的环境背景及自己尝试过哪些方法

python3.7
aiomyqsl 0.0.20

相关代码

class Test(object):
    async def _pool(self):
        self.pool = await aiomysql.create_pool(**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())

你期待的结果是什么?实际看到的错误信息又是什么?

执行结果
image.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文