def search_event(): # 连接数据库 conn_1 = Mysql_conn() # 构造SQL语句 sw = 'SELECT * FROM STUDENT ORDER BY ID' stu_info = conn_1.select(sw) # 保存当前首位符合条件的学员的ID global cur_search_id cur_search_id = stu_info[0][0] for items in stu_info: node = [] node.append(items[0]) node.append(items[1]) node.append(items[2]) node.append(items[3]) node.append(items[4]) node.append(items[5]) self.dl.add(node) key = search_listbox.curselection() value = search_entry.get() # 删除搜索框中数据 search_entry.delete(0, END) results = self.dl.find(value, key[0]) if len(results) == 0: messagebox.showinfo("查找提示", "未找到该学员!") show_listbox = Listbox(root, height=8, width=110) for row in results: show_listbox.insert(END, row) show_listbox.grid(row=8, column=0, columnspan=10) # 清空链表 self.dl.release() # 关闭数据库连接 conn_1.close_conn()
def upload_event(): i = [] # 连接数据库 conn_1 = Mysql_conn() # root.update() i.append(upload_entry_1.get()) i.append(upload_entry_2.get()) i.append(upload_entry_3.get()) i.append(upload_entry_4.get()) i.append(upload_entry_5.get()) # 清除输入框中的数据 upload_entry_1.delete(0, END) upload_entry_2.delete(0, END) upload_entry_3.delete(0, END) upload_entry_4.delete(0, END) upload_entry_5.delete(0, END) # 构造SQL语句 uw = "INSERT INTO STUDENT (NAME, AGE, TEL_NUMBER, DATE, EMAIL)" \ "VALUES('%s', '%s', '%s', '%s', '%s')" %(i[0], i[1], i[2], i[3], i[4]) conn_1.insert(uw) messagebox.showinfo("保存提示", "已保存学员信息!") # 刷新信息区 sw = 'SELECT * FROM STUDENT ORDER BY ID' stu_info = conn_1.select(sw) show_listbox = Listbox(root, height=8, width=110) for row in stu_info: show_listbox.insert(END, row) show_listbox.grid(row=8, column=0, columnspan=10) # 关闭数据库连接 conn_1.close_conn()
def update_event(): i = [] # 连接数据库 conn_1 = Mysql_conn() i.append(update_entry_1.get()) i.append(update_entry_2.get()) i.append(update_entry_3.get()) i.append(update_entry_4.get()) i.append(update_entry_5.get()) i.append(update_entry_6.get()) # 清除输入框中的数据 update_entry_1.delete(0, END) update_entry_2.delete(0, END) update_entry_3.delete(0, END) update_entry_4.delete(0, END) update_entry_5.delete(0, END) update_entry_6.delete(0, END) # 构造SQL语句 uw = "UPDATE STUDENT SET NAME = '%s', AGE = '%s', TEL_NUMBER = '%s', DATE = '%s', EMAIL = '%s' WHERE " \ "ID = '%s'"% (i[1], i[2], i[3], i[4], i[5], i[0]) conn_1.update(uw) messagebox.showinfo("更新提示", "已更新学员信息!") # 刷新信息区 sw = 'SELECT * FROM STUDENT ORDER BY ID' stu_info = conn_1.select(sw) show_listbox = Listbox(root, height=8, width=110) for row in stu_info: show_listbox.insert(END, row) show_listbox.grid(row=8, column=0, columnspan=10) # 关闭数据库连接 conn_1.close_conn()
def sort_event(n): # 连接数据库 conn_1 = Mysql_conn() if n == 1: # 构造查询SQL sw = 'SELECT * FROM STUDENT ORDER BY ID' stu_info = conn_1.select(sw) show_listbox = Listbox(root, height=8, width=110) for row in stu_info: show_listbox.insert(END, row) show_listbox.grid(row=8, column=0, columnspan=10) messagebox.showinfo("排序", "已将学员按学号排序!") elif n == 2: # 构造查询SQL sw = 'SELECT * FROM STUDENT ORDER BY NAME' stu_info = conn_1.select(sw) show_listbox = Listbox(root, height=8, width=110) for row in stu_info: show_listbox.insert(END, row) show_listbox.grid(row=8, column=0, columnspan=10) messagebox.showinfo("排序", "已将学员按姓名排序!") elif n == 3: # 构造查询SQL sw = 'SELECT * FROM STUDENT ORDER BY AGE' stu_info = conn_1.select(sw) show_listbox = Listbox(root, height=8, width=110) for row in stu_info: show_listbox.insert(END, row) show_listbox.grid(row=8, column=0, columnspan=10) messagebox.showinfo("排序", "已将学员按年龄排序!") # 关闭数据库连接 conn_1.close_conn()
def delete_event(): # 连接数据库 conn_1 = Mysql_conn() global cur_search_id messagebox.showinfo("删除提示", "该学员信息已删除!") # 从数据库中删除该学员,以保持同步 rw = "DELETE FROM STUDENT WHERE ID = '%d'" % (cur_search_id) conn_1.delete(rw) # 将cur_search_id 置为 None,防止误删 cur_search_id = None # 断开数据库 conn_1.close_conn() # 刷新数据显示 Listbox(root, height=8, width=110).grid(row=8, column=0, columnspan=10)