添加mysql执行封装类
This commit is contained in:
parent
2e5c6fb994
commit
baebc35c9e
@ -2,6 +2,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
import datetime
|
import datetime
|
||||||
|
from lib.tools import MySQLDB,SQLiteHandler
|
||||||
|
|
||||||
|
|
||||||
# eta 接口token
|
# 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',
|
'DOESCRUD Index', 'WTRBM1 EEGC Index', 'FVHCM1 INDEX', 'doedtprd index', 'CFFDQMMN INDEX',
|
||||||
'C2403083739', 'C2404167878', 'C2403250571', 'lmcads03 lme comdty', 'GC1 COMB Comdty',
|
'C2403083739', 'C2404167878', 'C2403250571', 'lmcads03 lme comdty', 'GC1 COMB Comdty',
|
||||||
'C2404171822','C2404167855',
|
'C2404171822','C2404167855',
|
||||||
'W000825','W000826','G.IPE', # 美国汽柴油
|
# 'W000825','W000826','G.IPE', # 美国汽柴油
|
||||||
'S5131019','ID00135604','FSGAM1 Index','S5120408','ID00136724'] # 新加坡汽柴油
|
# 'S5131019','ID00135604','FSGAM1 Index','S5120408','ID00136724', # 新加坡汽柴油
|
||||||
|
]
|
||||||
|
|
||||||
# 临时写死用指定的列,与上面的edbcode对应,后面更改
|
# 临时写死用指定的列,与上面的edbcode对应,后面更改
|
||||||
edbnamelist = [
|
edbnamelist = [
|
||||||
@ -32,8 +34,8 @@ edbnamelist = [
|
|||||||
'C2403128043','C2403150124','FVHCM1 INDEX','doedtprd index','CFFDQMMN INDEX',
|
'C2403128043','C2403150124','FVHCM1 INDEX','doedtprd index','CFFDQMMN INDEX',
|
||||||
'C2403083739','C2404167878',
|
'C2403083739','C2404167878',
|
||||||
'GC1 COMB Comdty','C2404167855',
|
'GC1 COMB Comdty','C2404167855',
|
||||||
'A汽油价格','W000826','ICE柴油价格',
|
# 'A汽油价格','W000826','ICE柴油价格',
|
||||||
'新加坡(含硫0.05%) 柴油现货价','柴油:10ppm:国际市场:FOB中间价:新加坡(日)','Bloomberg Commodity Fair Value Singapore Mogas 92 Swap Month 1','97#汽油FOB新加坡现货价','无铅汽油:97#:国际市场:FOB中间价:新加坡(日)'
|
# '新加坡(含硫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_train = True # 是否训练
|
||||||
is_debug = False # 是否调试
|
is_debug = True # 是否调试
|
||||||
is_eta = True # 是否使用eta接口
|
is_eta = True # 是否使用eta接口
|
||||||
is_timefurture = True # 是否使用时间特征
|
is_timefurture = True # 是否使用时间特征
|
||||||
is_fivemodels = False # 是否使用之前保存的最佳的5个模型
|
is_fivemodels = False # 是否使用之前保存的最佳的5个模型
|
||||||
@ -205,10 +207,26 @@ is_update_eta = False # 预测结果上传到eta
|
|||||||
is_update_report = False # 是否上传报告
|
is_update_report = False # 是否上传报告
|
||||||
is_update_warning_data = 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 # 数据开始年份
|
start_year = 2013 # 数据开始年份
|
||||||
end_time = '' # 数据截取日期
|
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 # 是否删除周末数据
|
delweekenday = True if freq == 'B' else False # 是否删除周末数据
|
||||||
is_corr = False # 特征是否参与滞后领先提升相关系数
|
is_corr = False # 特征是否参与滞后领先提升相关系数
|
||||||
add_kdj = False # 是否添加kdj指标
|
add_kdj = False # 是否添加kdj指标
|
||||||
@ -239,6 +257,8 @@ dataset = 'yuanyoudataset' # 数据集文件夹
|
|||||||
|
|
||||||
# 数据库名称
|
# 数据库名称
|
||||||
db_name = os.path.join(dataset,'jbsh_yuanyou.db')
|
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}'
|
settings = f'{input_size}-{horizon}-{train_steps}--{k}-{data_set}-{y}'
|
||||||
# 获取日期时间
|
# 获取日期时间
|
||||||
|
@ -40,8 +40,8 @@ pdfmetrics.registerFont(TTFont('SimSun', 'SimSun.ttf'))
|
|||||||
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
|
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
|
||||||
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
|
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
|
||||||
|
|
||||||
# from config_jingbo import *
|
from config_jingbo import *
|
||||||
from config_juxiting import *
|
# from config_juxiting import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
72
lib/tools.py
72
lib/tools.py
@ -4,7 +4,6 @@ import numpy as np
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import seaborn as sns
|
import seaborn as sns
|
||||||
from config_jingbo import logger
|
|
||||||
from sklearn import metrics
|
from sklearn import metrics
|
||||||
import random, string, base64, hmac, hashlib
|
import random, string, base64, hmac, hashlib
|
||||||
from reportlab.pdfbase import pdfmetrics # 注册字体
|
from reportlab.pdfbase import pdfmetrics # 注册字体
|
||||||
@ -21,10 +20,11 @@ import smtplib
|
|||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import pymysql
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import messagebox
|
from tkinter import messagebox
|
||||||
|
|
||||||
|
global logger
|
||||||
def timeit(func):
|
def timeit(func):
|
||||||
'''计时装饰器'''
|
'''计时装饰器'''
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
@ -446,5 +446,73 @@ class SQLiteHandler:
|
|||||||
else:
|
else:
|
||||||
print(f"Column '{column_name}' already exists in table '{table_name}'.")
|
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__':
|
if __name__ == '__main__':
|
||||||
print('This is a tool, not a script.')
|
print('This is a tool, not a script.')
|
@ -1,15 +1,13 @@
|
|||||||
# 读取配置
|
# 读取配置
|
||||||
from config_jingbo import *
|
from config_jingbo import *
|
||||||
from lib.dataread 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
|
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 glob
|
||||||
import torch
|
import torch
|
||||||
torch.set_float32_matmul_precision("high")
|
torch.set_float32_matmul_precision("high")
|
||||||
|
|
||||||
sqlitedb = SQLiteHandler(db_name)
|
|
||||||
sqlitedb.connect()
|
|
||||||
|
|
||||||
|
|
||||||
def predict_main():
|
def predict_main():
|
||||||
|
Loading…
Reference in New Issue
Block a user