mysql.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Mysql Functions
  3. """
  4. import pymysql
  5. class MysqlClient(object):
  6. """
  7. MySQL工具, env默认prod版本
  8. """
  9. def __init__(self):
  10. mysql_config = {
  11. # "host": "rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com",
  12. "host": "rr-bp1l12ea7e9wgu947.mysql.rds.aliyuncs.com",
  13. "port": 3306, # 端口号
  14. "user": "wx2016_longvideo", # mysql用户名
  15. "passwd": "wx2016_longvideoP@assword1234", # mysql用户登录密码
  16. "db": "longvideo", # 数据库名
  17. "charset": "utf8mb4" # 如果数据库里面的文本是utf8编码的,charset指定是utf8
  18. }
  19. self.connection = pymysql.connect(
  20. host=mysql_config['host'], # 数据库IP地址,内网地址
  21. port=mysql_config['port'], # 端口号
  22. user=mysql_config['user'], # mysql用户名
  23. passwd=mysql_config['passwd'], # mysql用户登录密码
  24. db=mysql_config['db'], # 数据库名
  25. charset=mysql_config['charset'] # 如果数据库里面的文本是utf8编码的,charset指定是utf8
  26. )
  27. def select(self, sql):
  28. """
  29. 查询
  30. :param sql:
  31. :return:
  32. """
  33. cursor = self.connection.cursor()
  34. cursor.execute(sql)
  35. data = cursor.fetchall()
  36. return data
  37. def update(self, sql):
  38. """
  39. 插入
  40. :param sql:
  41. :return:
  42. """
  43. cursor = self.connection.cursor()
  44. try:
  45. res = cursor.execute(sql)
  46. self.connection.commit()
  47. return res
  48. except Exception as e:
  49. self.connection.rollback()
  50. def close(self):
  51. """
  52. 关闭连接
  53. """
  54. self.connection.close()
  55. #
  56. #
  57. # M = MysqlClient()
  58. # sql = "SELECT title from wx_video where id = '19591529';"
  59. # w = M.select(sql)
  60. # print(w)