| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 | 
							- # -*- coding: utf-8 -*-
 
- # @Author: wangkun
 
- # @Time: 2023/2/2
 
- """
 
- 数据库连接及操作
 
- """
 
- import os
 
- import logging
 
- import pymysql
 
- from dotenv import load_dotenv
 
- load_dotenv(verbose=True)
 
- env = os.getenv('env')
 
- class MysqlHelper(object):
 
-     def __init__(self):
 
-         if env == 'hk':
 
-             # 创建一个 Connection 对象,代表了一个数据库连接
 
-             connection = pymysql.connect(
 
-                 host="rm-j6cz4c6pt96000xi3.mysql.rds.aliyuncs.com",  # 数据库IP地址,内网地址
 
-                 # host="rm-j6cz4c6pt96000xi3lo.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
 
-                 port=3306,  # 端口号
 
-                 user="crawler",  # mysql用户名
 
-                 passwd="crawler123456@",  # mysql用户登录密码
 
-                 db="piaoquan-crawler",  # 数据库名
 
-                 # 如果数据库里面的文本是utf8编码的,charset指定是utf8
 
-                 charset="utf8")
 
-         elif env == 'prod':
 
-             # 创建一个 Connection 对象,代表了一个数据库连接
 
-             connection = pymysql.connect(
 
-                 host="rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com",  # 数据库IP地址,内网地址
 
-                 # host="rm-bp1159bu17li9hi94ro.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
 
-                 port=3306,  # 端口号
 
-                 user="crawler",  # mysql用户名
 
-                 passwd="crawler123456@",  # mysql用户登录密码
 
-                 db="piaoquan-crawler",  # 数据库名
 
-                 # 如果数据库里面的文本是utf8编码的,charset指定是utf8
 
-                 charset="utf8")
 
-         else:
 
-             # 创建一个 Connection 对象,代表了一个数据库连接
 
-             connection = pymysql.connect(
 
-                 host="rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com",  # 数据库IP地址,内网地址
 
-                 # host="rm-bp1k5853td1r25g3ndo.mysql.rds.aliyuncs.com",  # 数据库IP地址,外网地址
 
-                 port=3306,  # 端口号
 
-                 user="crawler",  # mysql用户名
 
-                 passwd="crawler123456@",  # mysql用户登录密码
 
-                 db="piaoquan-crawler",  # 数据库名
 
-                 # 如果数据库里面的文本是utf8编码的,charset指定是utf8
 
-                 charset="utf8")
 
-         self.connection = connection
 
-         self.cursor = connection.cursor(cursor=pymysql.cursors.DictCursor)
 
-     def get_values(self, sql):
 
-         try:
 
-             self.cursor.execute(sql)
 
-             # fetchall方法返回的是一个元组,里面每个元素也是元组,代表一行记录
 
-             data = self.cursor.fetchall()
 
-             # 关闭数据库连接
 
-             # self.connection.close()
 
-             # 返回查询结果,元组
 
-             return data
 
-         except Exception as e:
 
-             logging.error(f"get_values异常:{e}\n")
 
-     def insert_values(self, sql, value):
 
-         try:
 
-             # 连接数据库
 
-             # 执行 sql 语句
 
-             self.cursor.execute(sql, value)
 
-             task_id = self.connection.insert_id()
 
-             self.connection.commit()
 
-             # 关闭数据库连接
 
-             # self.connection.close()
 
-             # 返回查询结果,元组
 
-             return task_id
 
-         except Exception as e:
 
-             logging.error(f"insert_values异常:{e}\n")
 
-     def update_values(self, sql):
 
-         try:
 
-             # 执行 sql 语句
 
-             self.cursor.execute(sql)
 
-             # 注意 一定要commit,否则添加数据不生效
 
-             self.connection.commit()
 
-             # self.connection.close()
 
-             return True
 
-         except Exception as e:
 
-             logging.error(f"update_values异常,进行回滚操作:{e}\n")
 
-             # 发生错误时回滚
 
-             self.connection.rollback()
 
-             # self.connection.close()
 
-             return False
 
-         # 关闭数据库连接
 
- if __name__ == "__main__":
 
-     MysqlHelper()
 
 
  |