Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

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

问题描述

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...