1234567891011121314151617181920212223242526272829303132333435363738 |
- import pymysql
- from config import set_config
- config_ = set_config()
- class MysqlHelper(object):
- def __init__(self):
- """
- 初始化mysql连接信息
- """
- self.mysql_info = config_.MYSQL_INFO
- def get_data(self, sql):
- """
- 查询数据
- :param sql: sql语句
- :return: data
- """
- # 连接数据库
- conn = pymysql.connect(**self.mysql_info)
- # 创建游标
- cursor = conn.cursor()
- try:
- # 执行SQL语句
- cursor.execute(sql)
- # 获取查询的所有记录
- data = cursor.fetchall()
- except Exception as e:
- return None
- # 关闭游标对象
- cursor.close()
- # 关闭数据库连接
- conn.close()
- return data
|