From baebc35c9e3c6b7e58caacd134a5e8c344ddf8df Mon Sep 17 00:00:00 2001 From: workpc Date: Mon, 2 Dec 2024 13:56:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mysql=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=B0=81=E8=A3=85=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config_jingbo.py | 32 +++++++++++++++++---- lib/dataread.py | 4 +-- lib/tools.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++-- main_yuanyou.py | 4 +-- 4 files changed, 99 insertions(+), 13 deletions(-) diff --git a/config_jingbo.py b/config_jingbo.py index faaf6c5..3f14ef2 100644 --- a/config_jingbo.py +++ b/config_jingbo.py @@ -2,6 +2,7 @@ import logging import os import logging.handlers import datetime +from lib.tools import MySQLDB,SQLiteHandler # eta 接口token @@ -21,8 +22,9 @@ edbcodelist = ['CO1 Comdty', 'ovx index', 'C2404194834', 'C2404199738', 'dxy cur 'DOESCRUD Index', 'WTRBM1 EEGC Index', 'FVHCM1 INDEX', 'doedtprd index', 'CFFDQMMN INDEX', 'C2403083739', 'C2404167878', 'C2403250571', 'lmcads03 lme comdty', 'GC1 COMB Comdty', 'C2404171822','C2404167855', - 'W000825','W000826','G.IPE', # 美国汽柴油 - 'S5131019','ID00135604','FSGAM1 Index','S5120408','ID00136724'] # 新加坡汽柴油 + # 'W000825','W000826','G.IPE', # 美国汽柴油 + # 'S5131019','ID00135604','FSGAM1 Index','S5120408','ID00136724', # 新加坡汽柴油 + ] # 临时写死用指定的列,与上面的edbcode对应,后面更改 edbnamelist = [ @@ -32,8 +34,8 @@ edbnamelist = [ 'C2403128043','C2403150124','FVHCM1 INDEX','doedtprd index','CFFDQMMN INDEX', 'C2403083739','C2404167878', 'GC1 COMB Comdty','C2404167855', - 'A汽油价格','W000826','ICE柴油价格', - '新加坡(含硫0.05%) 柴油现货价','柴油:10ppm:国际市场:FOB中间价:新加坡(日)','Bloomberg Commodity Fair Value Singapore Mogas 92 Swap Month 1','97#汽油FOB新加坡现货价','无铅汽油:97#:国际市场:FOB中间价:新加坡(日)' + # 'A汽油价格','W000826','ICE柴油价格', + # '新加坡(含硫0.05%) 柴油现货价','柴油:10ppm:国际市场:FOB中间价:新加坡(日)','Bloomberg Commodity Fair Value Singapore Mogas 92 Swap Month 1','97#汽油FOB新加坡现货价','无铅汽油:97#:国际市场:FOB中间价:新加坡(日)' ] @@ -195,7 +197,7 @@ warning_data = { ### 开关 is_train = True # 是否训练 -is_debug = False # 是否调试 +is_debug = True # 是否调试 is_eta = True # 是否使用eta接口 is_timefurture = True # 是否使用时间特征 is_fivemodels = False # 是否使用之前保存的最佳的5个模型 @@ -205,10 +207,26 @@ is_update_eta = False # 预测结果上传到eta is_update_report = False # 是否上传报告 is_update_warning_data = False # 是否上传预警数据 +### 北京环境数据库jbsh_test +# url: jdbc:mysql://192.168.101.27:3306/jingbo_test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true + +host = '192.168.101.27' +prot = 3306 +username ='root' +password = '123456' +dbname = 'jingbo_test' +table_name = 'v_tbl_crude_oil_warning' + +db_mysql = MySQLDB(host=host, user=username, password=password, database=dbname) + +# 连接到数据库 +db_mysql.connect() + + # 数据截取日期 start_year = 2013 # 数据开始年份 end_time = '' # 数据截取日期 -freq = 'M' # 时间频率,"D": 天 "W": 周"M": 月"Q": 季度"A": 年 "H": 小时 "T": 分钟 "S": 秒 "B": 工作日 +freq = 'B' # 时间频率,"D": 天 "W": 周"M": 月"Q": 季度"A": 年 "H": 小时 "T": 分钟 "S": 秒 "B": 工作日 delweekenday = True if freq == 'B' else False # 是否删除周末数据 is_corr = False # 特征是否参与滞后领先提升相关系数 add_kdj = False # 是否添加kdj指标 @@ -239,6 +257,8 @@ dataset = 'yuanyoudataset' # 数据集文件夹 # 数据库名称 db_name = os.path.join(dataset,'jbsh_yuanyou.db') +sqlitedb = SQLiteHandler(db_name) +sqlitedb.connect() settings = f'{input_size}-{horizon}-{train_steps}--{k}-{data_set}-{y}' # 获取日期时间 diff --git a/lib/dataread.py b/lib/dataread.py index 3c47d49..9fa62a2 100644 --- a/lib/dataread.py +++ b/lib/dataread.py @@ -40,8 +40,8 @@ pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf')) plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 -# from config_jingbo import * -from config_juxiting import * +from config_jingbo import * +# from config_juxiting import * diff --git a/lib/tools.py b/lib/tools.py index b8d7383..e80f343 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -4,7 +4,6 @@ import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns -from config_jingbo import logger from sklearn import metrics import random, string, base64, hmac, hashlib from reportlab.pdfbase import pdfmetrics # 注册字体 @@ -21,10 +20,11 @@ import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import sqlite3 +import pymysql import tkinter as tk from tkinter import messagebox - +global logger def timeit(func): '''计时装饰器''' def wrapper(*args, **kwargs): @@ -446,5 +446,73 @@ class SQLiteHandler: else: print(f"Column '{column_name}' already exists in table '{table_name}'.") +import logging +class MySQLDB: + def __init__(self, host, user, password, database): + self.host = host + self.user = user + self.password = password + self.database = database + self.connection = None + self.cursor = None + + def connect(self): + try: + self.connection = pymysql.connect( + host=self.host, + user=self.user, + password=self.password, + database=self.database, + charset='utf8mb4', + cursorclass=pymysql.cursors.DictCursor + ) + self.cursor = self.connection.cursor() + logging.info("Connected to the database successfully.") + except pymysql.Error as e: + logging.error(f"Error connecting to the database: {e}") + + def execute_query(self, query): + try: + self.cursor.execute(query) + result = self.cursor.fetchall() + return result + except pymysql.Error as e: + logging.error(f"Error executing query: {e}") + return None + + def execute_insert(self, query, values): + try: + self.cursor.execute(query, values) + self.connection.commit() + logging.info("Insert operation successful.") + except pymysql.Error as e: + logging.error(f"Error executing insert: {e}") + self.connection.rollback() + + def execute_update(self, query, values): + try: + self.cursor.execute(query, values) + self.connection.commit() + logging.info("Update operation successful.") + except pymysql.Error as e: + logging.error(f"Error executing update: {e}") + self.connection.rollback() + + def execute_delete(self, query, values): + try: + self.cursor.execute(query, values) + self.connection.commit() + logging.info("Delete operation successful.") + except pymysql.Error as e: + logging.error(f"Error executing delete: {e}") + self.connection.rollback() + + def close(self): + if self.cursor: + self.cursor.close() + if self.connection: + self.connection.close() + logging.info("Database connection closed.") + if __name__ == '__main__': print('This is a tool, not a script.') \ No newline at end of file diff --git a/main_yuanyou.py b/main_yuanyou.py index 8903b3d..b09d8f2 100644 --- a/main_yuanyou.py +++ b/main_yuanyou.py @@ -1,15 +1,13 @@ # 读取配置 from config_jingbo import * from lib.dataread import * -from lib.tools import * +# from lib.tools import * from models.nerulforcastmodels import ex_Model,model_losss,model_losss_juxiting,brent_export_pdf,tansuanli_export_pdf,pp_export_pdf,model_losss_juxiting import glob import torch torch.set_float32_matmul_precision("high") -sqlitedb = SQLiteHandler(db_name) -sqlitedb.connect() def predict_main():