__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """
  2. @author: luojunhui
  3. self.app.mysql_pool = await aiomysql.create_pool(
  4. host='rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com',
  5. port=3306,
  6. user='crawler',
  7. password='crawler123456@',
  8. db='piaoquan-crawler',
  9. charset='utf8mb4',
  10. connect_timeout=120,
  11. )
  12. """
  13. import aiomysql
  14. class AsyncMySQLClient(object):
  15. """
  16. Async MySQL
  17. """
  18. def __init__(self, app):
  19. self.app = app
  20. async def initPool(self):
  21. """
  22. 初始化连接
  23. host='',
  24. port=3306,
  25. user='changwen_admin',
  26. password='changwen@123456',
  27. db='long_articles',
  28. charset='utf8mb4'
  29. :return:
  30. """
  31. self.app.mysql_pool = await aiomysql.create_pool(
  32. host='rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
  33. port=3306,
  34. user='changwen_admin',
  35. password='changwen@123456',
  36. db='long_articles',
  37. charset='utf8mb4',
  38. connect_timeout=120,
  39. )
  40. print("mysql init successfully")
  41. async def closePool(self):
  42. """
  43. 关闭 mysql 连接
  44. :return:
  45. """
  46. self.app.mysql_pool.close()
  47. await self.app.mysql_pool.wait_closed()
  48. async def asyncSelect(self, sql):
  49. """
  50. select method
  51. :param sql:
  52. :return:
  53. """
  54. async with self.app.mysql_pool.acquire() as conn:
  55. async with conn.cursor() as cursor:
  56. await cursor.execute(sql)
  57. result = await cursor.fetchall()
  58. return result
  59. async def asyncInsert(self, sql, params):
  60. """
  61. insert and update method
  62. :param params:
  63. :param sql:
  64. :return:
  65. """
  66. async with self.app.mysql_pool.acquire() as coon:
  67. async with coon.cursor() as cursor:
  68. try:
  69. await cursor.execute(sql, params)
  70. await coon.commit()
  71. except Exception as e:
  72. await coon.rollback()
  73. raise
  74. class TaskMySQLClient(object):
  75. """
  76. Async MySQL
  77. """
  78. def __init__(self):
  79. self.mysql_pool = None
  80. async def init_pool(self):
  81. """
  82. 初始化连接
  83. :return:
  84. """
  85. self.mysql_pool = await aiomysql.create_pool(
  86. host='rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
  87. port=3306,
  88. user='changwen_admin',
  89. password='changwen@123456',
  90. db='long_articles',
  91. charset='utf8mb4',
  92. connect_timeout=120
  93. )
  94. print("mysql init successfully")
  95. async def close_pool(self):
  96. """
  97. 关闭 mysql 连接
  98. :return:
  99. """
  100. self.mysql_pool.close()
  101. await self.mysql_pool.wait_closed()
  102. async def asyncSelect(self, sql):
  103. """
  104. select method
  105. :param sql:
  106. :return:
  107. """
  108. async with self.mysql_pool.acquire() as conn:
  109. async with conn.cursor() as cursor:
  110. await cursor.execute(sql)
  111. result = await cursor.fetchall()
  112. return result
  113. async def asyncInsert(self, sql, params):
  114. """
  115. insert and update method
  116. :param params:
  117. :param sql:
  118. :return:
  119. """
  120. async with self.mysql_pool.acquire() as coon:
  121. async with coon.cursor() as cursor:
  122. await cursor.execute(sql, params)
  123. await coon.commit()