#设置窗口
def set_init_window(self):
self.init_window_name.title("SQLTool_v1.2") #窗口名
#self.init_window_name.geometry('320x160+10+10') #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
self.init_window_name.geometry('1440x900+10+10')
#self.init_window_name["bg"] = "pink" #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
#self.init_window_name.attributes("-alpha",0.9) #虚化,值越小虚化程度越高
#标签
self.init_data_label = Label(self.init_window_name, text="Input")
self.init_data_label.grid(row=0, column=0,sticky='w',padx=10)
self.result_data_label = Label(self.init_window_name, text="Result")
self.result_data_label.grid(row=13, column=0,sticky='w',padx=10)
#self.log_label = Label(self.init_window_name, text="日志")
#self.log_label.grid(row=12, column=0)
#文本框
self.init_data_Text = Text(self.init_window_name, width=200, height=30) #原始数据录入框
self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=22)
self.result_data_Text = Text(self.init_window_name, width=200, height=25) #处理结果展示
self.result_data_Text.grid(row=14, column=0, rowspan=10, columnspan=22)
#self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
#self.log_data_Text.grid(row=14, column=0, columnspan=10)
#SQL按钮
self.str_trans_to_md5_button = Button(self.init_window_name, text="Run", bg="lightblue", width=10,command=self.sql_param_set_main1) # 调用内部方法 加()为直接调用
self.str_trans_to_md5_button.grid(row=12, column=0,pady=10,sticky='w',padx=10)
#Clear按钮
self.str_trans_to_test_button = Button(self.init_window_name, text="Clear", bg="lightblue", width=10,command=self.clear) # 调用内部方法 加()为直接调用
self.str_trans_to_test_button.grid(row=12, column=1, pady=10, sticky='w', padx=10)
# Test按钮
self.str_trans_to_test_button = Button(self.init_window_name, text="ExcuteSQL_Run", bg="lightblue", width=10,command=self.sql_param_set_main2) # 调用内部方法 加()为直接调用
self.str_trans_to_test_button.grid(row=12, column=2, pady=10, sticky='w', padx=10)
#Test函数
def clear(self):
self.init_data_Text.delete(1.0, END)
self.result_data_Text.delete(1.0,END)
# 不可执行SQL(Date类型以字符串形式填充)
def sql_param_set_main1(self):
self.sql_param_set(False)
# 可执行SQL(Date类型自动转换)
def sql_param_set_main2(self):
self.sql_param_set(True)
#功能函数
def sql_param_set(self,flg):
self.result_data_Text.delete(1.0,END)
src = self.init_data_Text.get(1.0,END).strip()
#print("flg =", flg)
if src:
try:
sqlList = []
paramAllList = []
tableNameList = []
for line in src.split('\n'):
if 'Preparing' in line:
sql = line[line.find(': ') + 2:]
sqlList.append(sql)
sqlItems = sql.split(' ')
for sqlItemsIndex in range(len(sqlItems)):
if sqlItems[sqlItemsIndex] == 'from' or sqlItems[sqlItemsIndex] == 'join' or sqlItems[sqlItemsIndex] == 'into':
tableNameList.append(sqlItems[sqlItemsIndex + 1])
elif 'Parameters' in line:
param = line[line.find(': ') + 2:]
paramAllList.append(param)
else:
continue;
self.write_log_to_Text('---------------------SQL---------------------')
for sqlIndex in range(len(sqlList)):
param = paramAllList[sqlIndex]
sql = sqlList[sqlIndex]
paramList = param.split(', ')
self.write_log_to_Text('\n')
index = 0;
result_sql = '';
for i in range(len(sql)):
if 'call ' in sql:
continue;
if sql[i] == '?':
param = paramList[index];
if 'String' in param:
result_sql = result_sql + '\'' + param[:param.find('(')] + '\''
elif '(LocalDate)' in param or '(Date)' in param:
dateString = "'" + param[:param.find('(')].replace("-", "/") + "'"
if flg:
dateString = "to_date(" + dateString + ")"
result_sql = result_sql + dateString
else:
result_sql = result_sql + param[:param.find('(')]
index += 1;
else:
result_sql = result_sql + sql[i]
result_sql = result_sql + ";"
self.write_log_to_Text(result_sql)
self.write_log_to_Text('\n')
except:
self.result_data_Text.delete(1.0,END)
self.result_data_Text.insert(1.0,"failed!!")
else:
self.write_log_to_Text("ERROR:failed")
#获取当前时间
def get_current_time(self):
current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
return current_time
# 打印
def write_log_to_Text(self, logmsg):
#current_time = self.get_current_time()
logmsg_in = str(logmsg) # 换行 + "\n"
self.result_data_Text.insert(END, logmsg_in)
init_window.mainloop() #父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示