Ale*_*lex 5 python mysql python-asyncio aio-mysql
当我使用mysql_poolfromaiomysql更新一条数据时,第一次和第二次是一样的。
class Test(object):\n async def _pool(self):\n self.pool = await aiomysql.create_\xc3\xa7(**mysql_options)\n\n async def get_one(self, sql, param=None):\n await self.cur.execute(sql, param)\n result = await self.cur.fetchone()\n return result\n\n async def get(self):\n self.conn = await self.pool.acquire()\n self.cur = await self.conn.cursor(DictCursor)\n sql = \'\'\'select policy from tb_user where id = 2;\'\'\'\n res = await self.get_one(sql)\n print(res)\n await self.cur.close()\n await self.pool.release(self.conn)\n\n @staticmethod\n def update():\n import pymysql\n coon = pymysql.connect(host=\'127.0.0.1\',\n port=3306,\n user=mysql_options[\'user\'],\n autocommit=True,\n password=mysql_options[\'password\'],\n database=mysql_options[\'db\'])\n cursor = coon.cursor()\n sql = \'\'\'update tb_user set policy = 9 where id = 2;\'\'\'\n cursor.execute(sql)\n sql = \'\'\'select policy from tb_user where id = 2;\'\'\'\n cursor.execute(sql)\n data = cursor.fetchone()\n print(data)\n async def run(self):\n await self._pool()\n await self.get()\n self.update()\n await self.get()\n\n\nif __name__ == \'__main__\':\n test = Test()\n loop = asyncio.get_event_loop()\n loop.run_until_complete(test.run())\n\nRun Code Online (Sandbox Code Playgroud)\n版本
\n结果:
\n