"""
@Description: 数据库工具
"""
import pymysql
class DBManager:
"""
@ClassName: DBManager
@Description: 数据库管理
"""
tablename = 'mysql_test'
username = 'root'
password = '123456'
host = '127.0.0.1'
port = int(3306)
def __init__(self,host:str=host,username:str=username,password:str=password,tablename:str=tablename):
"""
@Description: 初始化
:param host 主机IP地址
:param username 数据库username
:param password 数据库密码
:param tablename 数据库名
"""
self.host = host
self.username = username
self.password = password
self.tablename = tablename
def exec_sql(self,sqlexpression):
"""
@Description: 执行sql语句
:param sqlexpression sql语句表达式
"""
try:
db = pymysql.connect(host=self.host,user=self.username,password=self.password,database=self.tablename)
cursor = db.cursor()
cursor.execute(sqlexpression)
data = cursor.fetchall()
print(data)
cursor.close()
db.close()
return data
except Exception as e:
print('数据库执行错误',e)
if __name__=='__main__':
db = DBManager()
print(db.exec_sql('select * from generatedatas_fakermethodnames'))
如有错误,欢迎私信或者评论指正
|