给oracle sqlldr 写了一个简单的gui
给sqlldr 写了一个简单的gui
用 cursor 简单写了一个GUI ,打个样,你们继续发挥。
import shutil
import subprocess
import tkinter as tk
from tkinter import filedialog, END
import os
class SQLldrGUI:
def __init__(self, master):
self.master = master
master.title("SQLldr GUI")
self.info_text = tk.Text(master)
self.info_text.pack()
self.info_text.configure(borderwidth=0)
self.table_label = tk.Label(master, text="Table Name:(要和数据表名大小写一致)")
self.table_label.pack()
self.table_entry = tk.Entry(master)
self.table_entry.pack()
self.file_label = tk.Label(master, text="File Path:(路径不能有中文)")
self.file_label.pack()
self.file_entry = tk.Entry(master)
self.file_entry.pack()
self.browse_button = tk.Button(master, text="Browse", command=self.browse_file)
self.browse_button.pack()
self.generate_button = tk.Button(master, text="Generate CTL", command=self.generate_ctl)
self.generate_button.pack()
self.preview_button = tk.Button(master, text="Preview CTL", command=self.preview_ctl)
self.preview_button.pack()
self.execute_button = tk.Button(master, text="Execute SQLldr (如果程序未响应是程序正在导入数据。)",
command=self.execute_sqlldr)
self.execute_button.pack()
def browse_file(self):
file_path = filedialog.askopenfilename()
self.file_entry.delete(0, tk.END)
self.file_entry.insert(0, file_path)
def generate_ctl(self):
table_name = self.table_entry.get()
file_path = self.file_entry.get()
if not table_name or not file_path:
self.info_text.insert(tk.END, "Table Name and File Path cannot be empty.\n")
with open(file_path, mode='r', encoding="GBK") as f: # 这个是导入的数据文件的编码自行修改。
first_line = f.readline().strip()
fields = first_line.split(',')
ctl_file = f"{table_name}.ctl"
with open(ctl_file, mode='w') as f:
f.write(f"--注意数据是以追加的方式导入。并行加载时只允许 APPEND 方式。\n")
f.write(f"options(skip=1, COLUMNARRAYROWS=10000, READSIZE=20971520, ERRORS=999999999)\n")
f.write(f"LOAD DATA\nINFILE '{file_path}'\n")
f.write(f"APPEND INTO TABLE \"{table_name}\"\n")
# append -- 追加的方式 导入表 还有 insert默认方式 、replace(等价delete from table语句) 、truncate(等价truncate table语句)
f.write(f"FIELDS TERMINATED BY ','\n")
f.write(f"Optionally enclosed by '\"'\n")
f.write(f"TRAILING NULLCOLS\n")
f.write(f"(")
i = 0
for field in fields:
field = field.replace("\"", "")
f.write(f"{field}")
i += 1
if not len(fields) == i:
f.write(f", \n")
print(len(fields), "个字段")
f.write(")\n")
with open(ctl_file, mode='r') as f:
content = f.read()
self.info_text.delete(0.0, END) # 清空文本框内容
self.info_text.insert(tk.END, content)
return ctl_file
def preview_ctl(self):
table_name = self.table_entry.get()
file_path = self.file_entry.get()
ctl_file = self.generate_ctl()
with open(ctl_file, mode='r') as f:
content = f.read()
self.preview_window = tk.Toplevel(self.master)
self.preview_window.title("Preview CTL")
self.preview_text = tk.Text(self.preview_window)
self.preview_text.pack()
self.preview_text.insert(tk.END, content)
self.save_button = tk.Button(self.preview_window, text="Save", command=lambda: self.save_ctl(ctl_file))
self.save_button.pack()
def save_ctl(self, ctl_file):
new_ctl_file = filedialog.asksaveasfilename(defaultextension=".ctl")
shutil.copyfile(ctl_file, new_ctl_file)
os.remove(ctl_file)
def execute_sqlldr(self):
self.info_text.delete(0.0, END) # 清空文本框内容
table_name = self.table_entry.get()
file_path = self.file_entry.get()
ctl_file = self.generate_ctl()
cmd = f"sqlldr userid=xxx/[email protected]:1521/orcl control={ctl_file} direct=true parallel=true log={ctl_file}.log"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
self.info_text.insert(tk.END, line.decode('GBK'))
root = tk.Tk()
root.geometry("550x520")
my_gui = SQLldrGUI(root)
root.mainloop()