parent
6ed595a3d9
commit
fcaa13bf40
@ -0,0 +1,117 @@
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox, Toplevel, ttk, simpledialog
|
||||
import pymysql.cursors
|
||||
|
||||
# 数据库连接配置
|
||||
connection = pymysql.connect(host='localhost',
|
||||
user='root',
|
||||
password='123',
|
||||
database='mybatis',
|
||||
cursorclass=pymysql.cursors.DictCursor)
|
||||
|
||||
# 定义表结构
|
||||
tables_schema = {
|
||||
"Student": ["StudentID", "Name", "Gender", "Birthdate", "ContactInfo", "MajorID"],
|
||||
"Teacher": ["TeacherID", "Name", "Gender", "Birthdate", "ContactInfo", "Title"],
|
||||
"Course": ["CourseID", "CourseName", "Credits", "TeacherID"],
|
||||
"Grade": ["GradeID", "StudentID", "CourseID", "Score"],
|
||||
"Enrollment": ["EnrollmentID", "StudentID", "CourseID", "EnrollmentDate"],
|
||||
"Major": ["MajorID", "MajorName", "CollegeID"],
|
||||
"College": ["CollegeID", "CollegeName"],
|
||||
"Log": ["LogID", "OperationType", "OperationTime", "Operator"],
|
||||
"Administrator": ["AdminID", "Name", "ContactInfo", "Logging"]
|
||||
}
|
||||
|
||||
# 查询操作
|
||||
def query_table(table_name):
|
||||
sql = f"SELECT * FROM {table_name}"
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(sql)
|
||||
result = cursor.fetchall()
|
||||
for row in result:
|
||||
print(row)
|
||||
connection.commit()
|
||||
messagebox.showinfo("Query Result", f"Query successful for table {table_name}")
|
||||
|
||||
# 插入或更新操作
|
||||
def insert_or_update_data(table_name, operation):
|
||||
def submit():
|
||||
values = {field: entry.get() for field, entry in entries.items()}
|
||||
sql = f"{operation} INTO {table_name} ({', '.join(values.keys())}) VALUES ({', '.join(['%s'] * len(values))})"
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(sql, list(values.values()))
|
||||
connection.commit()
|
||||
messagebox.showinfo("Success", f"{operation.capitalize()} operation successful")
|
||||
top.destroy()
|
||||
|
||||
top = Toplevel(root)
|
||||
top.title(f"{operation.capitalize()} {table_name}")
|
||||
|
||||
entries = {}
|
||||
for i, field in enumerate(tables_schema[table_name]):
|
||||
label = ttk.Label(top, text=f"{field}:")
|
||||
label.grid(row=i, column=0, sticky='e', padx=5, pady=5)
|
||||
entry = ttk.Entry(top)
|
||||
entry.grid(row=i, column=1, padx=5, pady=5)
|
||||
entries[field] = entry
|
||||
|
||||
submit_button = ttk.Button(top, text="Submit", command=submit)
|
||||
submit_button.grid(row=len(tables_schema[table_name]), column=0, columnspan=2, padx=5, pady=5)
|
||||
|
||||
# 删除操作
|
||||
def delete_data(table_name):
|
||||
condition = simpledialog.askstring("Input", f"Enter delete condition for {table_name} (e.g., id = 1):")
|
||||
sql = f"DELETE FROM {table_name} WHERE {condition}"
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(sql)
|
||||
connection.commit()
|
||||
messagebox.showinfo("Success", "Data deleted successfully")
|
||||
|
||||
# 选择表并执行操作
|
||||
def choose_table_and_operate():
|
||||
table_name = table_var.get()
|
||||
operation = operation_var.get()
|
||||
if operation == "Query":
|
||||
query_table(table_name)
|
||||
elif operation in ["Insert", "Update"]:
|
||||
insert_or_update_data(table_name, operation)
|
||||
elif operation == "Delete":
|
||||
delete_data(table_name)
|
||||
|
||||
# 关闭应用程序
|
||||
def close_application():
|
||||
if messagebox.askokcancel("Quit", "Do you want to quit?"):
|
||||
root.destroy()
|
||||
|
||||
# 创建主窗口
|
||||
root = tk.Tk()
|
||||
root.title("Database Operations")
|
||||
root.geometry("500x400")
|
||||
|
||||
style = ttk.Style()
|
||||
style.theme_use('clam')
|
||||
|
||||
# 创建表格名称变量和操作变量
|
||||
table_var = tk.StringVar()
|
||||
operation_var = tk.StringVar()
|
||||
|
||||
# 创建标签和下拉列表框
|
||||
ttk.Label(root, text="Choose Table:").pack(pady=10)
|
||||
table_option = ttk.Combobox(root, textvariable=table_var, values=list(tables_schema.keys()))
|
||||
table_option.pack(pady=10)
|
||||
|
||||
ttk.Label(root, text="Choose Operation:").pack(pady=10)
|
||||
operations = ["Query", "Insert", "Update", "Delete"]
|
||||
operation_option = ttk.Combobox(root, textvariable=operation_var, values=operations)
|
||||
operation_option.pack(pady=10)
|
||||
|
||||
# 创建操作按钮
|
||||
operate_button = ttk.Button(root, text="Operate", command=choose_table_and_operate)
|
||||
operate_button.pack(pady=20)
|
||||
|
||||
# 创建关闭按钮
|
||||
close_button = ttk.Button(root, text="Close", command=close_application)
|
||||
close_button.pack(pady=20)
|
||||
|
||||
# 启动事件循环
|
||||
root.mainloop()
|
||||
Loading…
Reference in new issue