feat(session): 项目基本完成
- 新增热加载模块funcs.py - 新增文件加载模块files.py - 新增了日志打印 - 新增其他功能
This commit is contained in:
45
commons/databases.py
Normal file
45
commons/databases.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
"""
|
||||
@author: CNWei
|
||||
@Software: PyCharm
|
||||
@contact: t6i888@163.com
|
||||
@file: databases
|
||||
@date: 2025/2/16 20:53
|
||||
@desc:
|
||||
"""
|
||||
import logging
|
||||
import pymysql as MySQLdb
|
||||
|
||||
from commons import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DBServer:
|
||||
def __init__(self, host, port, user, password, database):
|
||||
self.db = MySQLdb.connect(host=host, port=port, user=user, password=password, database=database)
|
||||
self.cursor = self.db.cursor() # 创建新的会话
|
||||
|
||||
def execute_sql(self, sql):
|
||||
logger.info(f"执行sql:{sql}")
|
||||
self.cursor.execute(sql) # 执行sql命令
|
||||
|
||||
# res = self.cursor.fetchone() # 返回单行结果
|
||||
res = self.cursor.fetchall() # 返回多行结果
|
||||
return res
|
||||
|
||||
|
||||
db = DBServer(
|
||||
host=settings.db_host, # ip
|
||||
port=3306, # 端口
|
||||
user='root', # 用户名
|
||||
password='mysql_hNahSe', # 密码
|
||||
database='answer' # 库名
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
...
|
||||
res = db.execute_sql('select username from user where id=1;')
|
||||
print(res[0])
|
||||
Reference in New Issue
Block a user