|
@@ -10,6 +10,7 @@ import decimal
|
|
|
import traceback
|
|
|
import pymysql
|
|
|
import os
|
|
|
+from typing import List, Tuple
|
|
|
from src.core import singleton
|
|
|
from redis import StrictRedis, ConnectionPool
|
|
|
|
|
@@ -36,14 +37,14 @@ class MysqlHandler:
|
|
|
"""
|
|
|
|
|
|
def __init__(self,
|
|
|
- # host='10.0.0.28',
|
|
|
- # user='root',
|
|
|
- # passwd='Hongshan2024@longjiang',
|
|
|
-
|
|
|
- host=MYSQL_HOST,
|
|
|
+ host='172.16.12.16',
|
|
|
user='root',
|
|
|
- passwd=MYSQL_PASSWORD,
|
|
|
- db='big_model',
|
|
|
+ passwd='12345678',
|
|
|
+
|
|
|
+ # host=MYSQL_HOST,
|
|
|
+ # user='root',
|
|
|
+ # passwd=MYSQL_PASSWORD,
|
|
|
+ db='libra_bot',
|
|
|
port=3306,
|
|
|
charset='utf8'
|
|
|
):
|
|
@@ -79,18 +80,18 @@ class MysqlHandler:
|
|
|
_number += 1
|
|
|
time.sleep(stime) # 连接不成功,休眠3秒钟,继续循环,知道成功或重试次数结束
|
|
|
|
|
|
- def query(self, sql):
|
|
|
+ def select(self, query, params: Tuple = None):
|
|
|
try:
|
|
|
self._reConn()
|
|
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
|
|
cursor = self.conn.cursor(pymysql.cursors.DictCursor)
|
|
|
# 执行SQL语句
|
|
|
- cursor.execute(sql)
|
|
|
+ cursor.execute(query, params)
|
|
|
# 获取所有记录列表
|
|
|
results = cursor.fetchall()
|
|
|
return results
|
|
|
except:
|
|
|
- print(sql)
|
|
|
+ print(query, params)
|
|
|
traceback.print_exc()
|
|
|
finally:
|
|
|
# 增/删/改均需要进行commit提交,进行保存
|
|
@@ -98,18 +99,81 @@ class MysqlHandler:
|
|
|
# 关闭游标
|
|
|
cursor.close()
|
|
|
|
|
|
- def exec(self, sql):
|
|
|
+ def execute(self, sql, values: Tuple = None):
|
|
|
try:
|
|
|
+ self._reConn()
|
|
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
|
|
- cursor = self.db.cursor()
|
|
|
+ cursor = self.conn.cursor()
|
|
|
# 执行SQL语句
|
|
|
- cursor.execute(sql)
|
|
|
- self.db.commit()
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
+ cursor.execute(sql, values)
|
|
|
+ self.conn.commit()
|
|
|
+ return cursor.rowcount, cursor.lastrowid
|
|
|
+ except:
|
|
|
+ print(sql, values)
|
|
|
+ traceback.print_exc()
|
|
|
+ raise e
|
|
|
finally:
|
|
|
cursor.close()
|
|
|
|
|
|
+ def create_database(self, db_name: str):
|
|
|
+ sql = f"CREATE DATABASE IF NOT EXISTS `{db_name}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
|
|
|
+ try:
|
|
|
+ self.execute(sql)
|
|
|
+ print(f"数据库'{db_name}'创建成功")
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
+ print(f"创建数据库时发生错误: {e}")
|
|
|
+
|
|
|
+ def use_database(self, db_name: str):
|
|
|
+ try:
|
|
|
+ self.conn.select_db(db_name)
|
|
|
+ except pymysql.ProgrammingError as e:
|
|
|
+ print(f"切换至数据库'{db_name}'时发生错误: {e}")
|
|
|
+
|
|
|
+ def create_table(self, table_schema: str):
|
|
|
+ try:
|
|
|
+ self.execute(table_schema)
|
|
|
+ print("表创建成功")
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
+ print(f"创建表时发生错误: {e}")
|
|
|
+
|
|
|
+ def insert(self, table_name: str, values: Tuple, columns: Tuple[str, ...] = None):
|
|
|
+ placeholders = ', '.join(['%s'] * len(values))
|
|
|
+ if columns:
|
|
|
+ columns_str = ', '.join(columns)
|
|
|
+ sql = f"INSERT INTO `{table_name}` ({columns_str}) VALUES ({placeholders})"
|
|
|
+ else:
|
|
|
+ sql = f"INSERT INTO `{table_name}` VALUES ({placeholders})"
|
|
|
+
|
|
|
+ try:
|
|
|
+ rowcount, lastrowid = self.execute(sql, values)
|
|
|
+ return lastrowid
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
+ print(f"插入数据时发生错误: {e}")
|
|
|
+
|
|
|
+ def update(self, table_name: str, set_clause: str, where_clause: str, values: Tuple):
|
|
|
+ sql = f"UPDATE `{table_name}` SET {set_clause} WHERE {where_clause}"
|
|
|
+ try:
|
|
|
+ rowcount, lastrowid = self.execute(sql, values)
|
|
|
+ return rowcount
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
+ print(f"更新数据时发生错误: {e}")
|
|
|
+
|
|
|
+ def delete(self, table_name: str, where_clause: str, values: Tuple):
|
|
|
+ sql = f"DELETE FROM `{table_name}` WHERE {where_clause}"
|
|
|
+ try:
|
|
|
+ rowcount, lastrowid = self.execute(sql, values)
|
|
|
+ return rowcount
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
+ print(f"删除数据时发生错误: {e}")
|
|
|
+
|
|
|
+ def select_one(self, query: str, params: Tuple = None):
|
|
|
+ try:
|
|
|
+ result = self.select(query, params)
|
|
|
+ if result and len(result) > 0:
|
|
|
+ return result[0]
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
+ print(f"执行查询时发生错误: {e}")
|
|
|
+
|
|
|
def __del__(self):
|
|
|
self.conn.close()
|
|
|
|
|
@@ -117,7 +181,7 @@ class MysqlHandler:
|
|
|
@singleton
|
|
|
class RedisHandler:
|
|
|
|
|
|
- def __init__(self, host='192.168.100.195', port=6379, db=0, password='^YHN&UJM'):
|
|
|
+ def __init__(self, host='172.16.12.16', port=6379, db=0, password='^YHN&UJM'):
|
|
|
try:
|
|
|
# host = '10.0.0.24'
|
|
|
# host = RADIS_HOST
|
|
@@ -162,15 +226,41 @@ class RedisHandler:
|
|
|
# # # 在这里可以添加任何自定义的处理逻辑
|
|
|
#
|
|
|
#
|
|
|
-# def main1():
|
|
|
-# mysql = MysqlHandler()
|
|
|
-# count = mysql.query("select count(0) as totals from t_xinyi_industry where TEST_TIME like '%00'")
|
|
|
-# print(count)
|
|
|
-#
|
|
|
-# rows = mysql.query("select * from t_xinyi_industry where TEST_TIME like '%00' limit 10")
|
|
|
-# for idx, cols in enumerate(rows):
|
|
|
-# print(cols.get('TEST_TIME'), cols.get('JS_COD'))
|
|
|
-#
|
|
|
-#
|
|
|
-# if __name__ == "__main__":
|
|
|
-# main()
|
|
|
+def main1():
|
|
|
+ mysql = MysqlHandler()
|
|
|
+ rows = mysql.select_one("select * from c_agent")
|
|
|
+ print(rows)
|
|
|
+
|
|
|
+ # # 创建数据库
|
|
|
+ # ds.create_database('new_database')
|
|
|
+ #
|
|
|
+ # # 切换到新数据库
|
|
|
+ # ds.use_database('new_database')
|
|
|
+ #
|
|
|
+ # # 创建表
|
|
|
+ # table_schema = """
|
|
|
+ # CREATE TABLE IF NOT EXISTS test_table (
|
|
|
+ # id INT AUTO_INCREMENT PRIMARY KEY,
|
|
|
+ # name VARCHAR(100),
|
|
|
+ # email VARCHAR(100),
|
|
|
+ # created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
+ # );
|
|
|
+ # """
|
|
|
+ # ds.create_table(table_schema)
|
|
|
+ #
|
|
|
+ # # 插入数据
|
|
|
+ # ds.insert('test_table', ('John Doe', 'john.doe@example.com'))
|
|
|
+ #
|
|
|
+ # # 更新数据
|
|
|
+ # set_clause = "name = %s, email = %s"
|
|
|
+ # where_clause = "id = %s"
|
|
|
+ # values = ('Jane Doe', 'jane.doe@example.com', 1)
|
|
|
+ # ds.update('test_table', set_clause, where_clause, values)
|
|
|
+ #
|
|
|
+ # # 删除数据
|
|
|
+ # where_clause = "id = %s"
|
|
|
+ # values = (1,)
|
|
|
+ # ds.delete('test_table', where_clause, values)
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main1()
|