mysql_utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. Author renyupeng
  5. coding=utf-8
  6. @Time : 2023/2/7 下午
  7. @Site :
  8. @File : mysql_utils.py
  9. @Software: PyCharm
  10. @contact: renyupeng@c-top.com.cn
  11. @Tel 1501435553
  12. encoding=utf-8
  13. """
  14. import pymysql
  15. # 导入所有Mysql配置常量,请自行指定文件
  16. from constant.ConfConstant import ConfConstant
  17. class MysqlUtils(object):
  18. """
  19. mysql操作类,对mysql数据库进行增删改查
  20. """
  21. def __init__(self):
  22. config = dict(
  23. host=ConfConstant.TIDB_PRO_HOST,
  24. db=ConfConstant.TIDB_PRO_DB,
  25. user=ConfConstant.TIDB_PRO_USER,
  26. password=ConfConstant.TIDB_PRO_PASSWORD,
  27. port=ConfConstant.TIDB_PRO_PORT,
  28. charset='utf8mb4', # 编码要加上,否则可能出现中文乱码问题
  29. use_unicode=False,
  30. )
  31. # Connect to the database
  32. self.connection = pymysql.connect(**config)
  33. self.connection.autocommit(True)
  34. self.cursor = self.connection.cursor()
  35. def QueryAll(self, sql):
  36. """
  37. 查询所有数据
  38. :param sql:
  39. :return:
  40. """
  41. # 数据库若断开即重连
  42. self.reConnect()
  43. self.cursor.execute(sql)
  44. return self.cursor.fetchall()
  45. def QueryMany(self, sql, n):
  46. """
  47. 查询某几条数据数据
  48. :param sql:
  49. :return:
  50. """
  51. # 数据库若断开即重连
  52. self.reConnect()
  53. self.cursor.execute(sql)
  54. return self.cursor.fetchmany(n)
  55. def QueryOne(self, sql):
  56. """
  57. 查询某几条数据数据
  58. :param sql:
  59. :return:
  60. """
  61. # 数据库若断开即重连
  62. self.reConnect()
  63. self.cursor.execute(sql)
  64. return self.cursor.fetchone()
  65. # return self.cursor.fetchone()
  66. def reConnect(self):
  67. """
  68. 重连机制
  69. :return:
  70. """
  71. try:
  72. self.connection.ping()
  73. except:
  74. self.connection()
  75. def Operate(self, sql, params=None, DML=True):
  76. """
  77. 数据库操作:增删改查
  78. DML: insert / update / delete
  79. DDL: CREATE TABLE/VIEW/INDEX/SYN/CLUSTER
  80. """
  81. try:
  82. # 数据库若断开即重连
  83. self.reConnect()
  84. with self.connection.cursor() as cursor:
  85. cursor.execute(sql, params)
  86. self.connection.commit()
  87. except Exception as e:
  88. if DML:
  89. # 涉及DML操作时,若抛异常需要回滚
  90. self.connection.rollback()
  91. print(e)
  92. def __del__(self):
  93. """
  94. MysqlConnection实例对象被释放时调用此方法,用于关闭cursor和connection连接
  95. """
  96. self.cursor.close()
  97. self.connection.close()