1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- """
- @author: luojunhui
- """
- import aiomysql
- class AsyncMySQLClient(object):
- """
- Async MySQL
- """
- def __init__(self, app):
- self.app = app
- async def init_pool(self):
- """
- 初始化连接
- :return:
- """
- self.app.mysql_pool = await aiomysql.create_pool(
- host='rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com',
- port=3306,
- user='crawler',
- password='crawler123456@',
- db='piaoquan-crawler',
- charset='utf8mb4'
- )
- print("mysql init successfully")
- async def close_pool(self):
- """
- 关闭 mysql 连接
- :return:
- """
- self.app.mysql_pool.close()
- await self.app.mysql_pool.wait_closed()
- async def async_select(self, sql):
- """
- select method
- :param sql:
- :return:
- """
- async with self.app.mysql_pool.acquire() as conn:
- async with conn.cursor() as cursor:
- await cursor.execute(sql)
- result = await cursor.fetchall()
- return result
- async def async_insert(self, sql):
- """
- insert and update method
- :param sql:
- :return:
- """
- async with self.app.mysql_pool.acquire() as coon:
- async with coon.cursor() as cursor:
- await cursor.execute(sql)
- await coon.commit()
|