12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- # -*- coding: utf-8 -*-
- # @Time: 2023/12/26
- """
- 数据库连接及操作
- """
- import redis
- import pymysql
- from common.common import Common
- # from common import Common
- class MysqlHelper:
- @classmethod
- def connect_mysql(cls):
- # 创建一个 Connection 对象,代表了一个数据库连接
- connection = pymysql.connect(
- # host="rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com",# 数据库IP地址,内网地址
- host="rr-bp1l12ea7e9wgu947.mysql.rds.aliyuncs.com",
- # host="rm-j6cz4c6pt96000xi3lo.mysql.rds.aliyuncs.com",# 数据库IP地址,外网地址
- port=3306, # 端口号
- user="longvideo_read", # mysql用户名
- passwd="wx2016_longvideoP@assword1234", # mysql用户登录密码
- db="longvideo", # 数据库名
- # 如果数据库里面的文本是utf8编码的,charset指定是utf8
- charset = "utf8")
- return connection
- @classmethod
- def get_values(cls, sql):
- try:
- machine = ""
- # 连接数据库
- connect = cls.connect_mysql()
- # 返回一个 Cursor对象
- mysql = connect.cursor()
- # 执行 sql 语句
- mysql.execute(sql)
- # fetchall方法返回的是一个元组,里面每个元素也是元组,代表一行记录
- data = mysql.fetchall()
- # 关闭数据库连接
- connect.close()
- # 返回查询结果,元组
- return data
- except Exception as e:
- print(f"get_values异常:{e}\n")
- if __name__ == "__main__":
- pass
|