如下代码用tkinter做了个人员管理界面,但是 在修改页面修改数据时提示UPDATE成功,可是数据库没有改变,麻烦帮忙看看,谢谢
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.dialogs import Messagebox
import PIL.Image
import PIL.ImageTk
import sqlite3
import time
root = ttk.Window()
tv = ttk.Treeview(
master=root,
columns=[0, 1, 2, 3, 4, 5, 6, 7, 8],
show=HEADINGS,
height=10,
bootstyle='dark'
)
# 从数据库中读取数据添加到table_data[]
conn = sqlite3.connect('./datadb/test1.db')
cur = conn.cursor()
sqlstr = 'select * from tab_person'
try:
cur.execute(sqlstr)
datatab = cur.fetchall()
# print(datatab)
table_data = []
for i in datatab:
table_data.append(i)
# print(table_data)
except Exception as e:
print(e)
print('查询数据不存在')
finally:
cur.close()
conn.close()
for row in table_data:
tv.insert('', END, values=row)
# print(tv.get_children())#('I001', 'I002', 'I003', 'I004', 'I005')
# tv.selection_set('I003')
tv.heading(0, text='ID') # 序号
tv.heading(1, text='NAME') # 姓名
tv.heading(2, text='SEX') # 性别
tv.heading(3, text='TEL') # 电话
tv.heading(4, text='Birthday') # 生日
tv.heading(5, text='Police_ID') # 警号
tv.heading(6, text='P_ID') # 身份证号码
tv.heading(7, text='Department') # 部门、单位
tv.heading(8, text='Address') # 地址
tv.column(0, width=60, anchor=CENTER)
tv.column(1, width=100, anchor=CENTER)
tv.column(2, width=100, anchor=CENTER)
tv.column(3, width=100, anchor=CENTER)
tv.column(4, width=100, anchor=CENTER)
tv.column(5, width=100, anchor=CENTER)
tv.column(6, width=200, anchor=CENTER)
tv.column(7, width=200, anchor=CENTER)
tv.column(8, width=200, anchor=CENTER)
# tv.pack(side=LEFT, anchor=NE, fill=X)
tv.grid(row=1, column=1)
var_entry = ttk.StringVar(value='输入选择的查询条件值')
# var_entry.set('输入选择的查询条件值')
select_entry = ttk.Entry(root, textvariable=var_entry, width=30).grid(row=2, column=1, sticky=ttk.W, padx=190)
def show(event):
# 使用var.get()来获得目前选项内容
varLabel.set(var_combobox.get())
var_combobox = ttk.StringVar()
# state 设置状态: normal(可选可输入)、readonly(只可选)、 disabled禁用
select_combobox = ttk.Combobox(root, textvariable=var_combobox, state=READONLY,
value=('请选择查询条件', '姓名', '警号', '身份证号', '电话号码'))
select_combobox.current(0)
select_combobox.bind('<>', show)
select_combobox.grid(row=2, column=1, sticky=ttk.W, padx=20)
varLabel = ttk.StringVar()
label = ttk.Label(root, textvariable=varLabel, width=6).grid(row=2, column=1, sticky=ttk.W, padx=465)
select_button = ttk.Button(text='查询').grid(row=2, column=1, sticky=ttk.W, padx=415)
def treeviewClick(event): # 单击
print('单击')
for item in tv.selection():
item_text = tv.item(item, "values")
new_window = ttk.Toplevel(root)
new_window.geometry('350x500')
ttk.Label(new_window,
text=f"编号:{item_text[0]}\n姓名:{item_text[1]}\n性别:{item_text[2]}\n电话:{item_text[3]}\n生日:{item_text[4]}\n警号:{item_text[5]}\n身份证号码:{item_text[6]}\n工作单位:{item_text[7]}\n地址:{item_text[8]}",
bootstyle=INFO).place(x=50, y=250)
img_path = ""
# print(len(img_path))
if len(img_path) == 0:
if item_text[2] == "女":
img_path = "./img/woman.png"
elif item_text[2] == "男":
img_path = "./img/man.png"
else:
img_path = img_path
im = PIL.Image.open(img_path).resize((150, 200), PIL.Image.BILINEAR)
photo = PIL.ImageTk.PhotoImage(im)
label = ttk.Label(new_window, image=photo, bootstyle="inverse-danger")
label.image = photo # keep a reference!
label.place(x=100, y=10)
print(tv.selection()) # Selection 方法返回 Treeview 中选中行的索引
'''----------------------------------------------------------------------------------------------------------------'''
def click_menu_edit():
# 以下代码是修改页面
global edit_temp, menu_edit_window
menu_edit_window = ttk.Toplevel(root)
menu_edit_window.geometry('350x500')
menu_edit_window.title('人员修改界面')
# print(tv.selection())#treeview选中行的索引
selected = tv.focus() # 获取焦点
# print(selected)
edit_temp = tv.item(selected, 'values')
# print(edit_temp[0])
user_name_label = ttk.Label(menu_edit_window, text="姓名:", width=5).grid(row=0, column=15)
user_name_entry = ttk.Entry(menu_edit_window, width=30)
user_name_entry.insert(0, edit_temp[1])
user_name_entry.grid(row=0, column=25)
user_sex_label = ttk.Label(menu_edit_window, text="性别:", width=5).grid(row=4, column=15)
user_sex_entry = ttk.Entry(menu_edit_window, width=30)
user_sex_entry.insert(0, edit_temp[2])
user_sex_entry.grid(row=4, column=25)
user_tel_label = ttk.Label(menu_edit_window, text="电话:", width=5).grid(row=8, column=15)
user_tel_entry = ttk.Entry(menu_edit_window, width=30)
user_tel_entry.insert(0, edit_temp[3])
user_tel_entry.grid(row=8, column=25, )
user_birthday_label = ttk.Label(menu_edit_window, text="生日:", width=5).grid(row=12, column=15)
user_birthday_entry = ttk.Entry(menu_edit_window, width=30)
user_birthday_entry.insert(0, edit_temp[4])
user_birthday_entry.grid(row=12, column=25, )
user_policeid_label = ttk.Label(menu_edit_window, text="警号:", width=5).grid(row=16, column=15)
user_policeid_entry = ttk.Entry(menu_edit_window, width=30)
user_policeid_entry.insert(0, edit_temp[5])
user_policeid_entry.grid(row=16, column=25, )
user_personid_label = ttk.Label(menu_edit_window, text="身份证号:", width=10).grid(row=20, column=15)
user_personid_entry = ttk.Entry(menu_edit_window, width=30)
user_personid_entry.insert(0, edit_temp[6])
user_personid_entry.grid(row=20, column=25)
user_department_label = ttk.Label(menu_edit_window, text="所属单位:", width=10).grid(row=24, column=15)
user_department_entry = ttk.Entry(menu_edit_window, width=30)
user_department_entry.insert(0, edit_temp[7])
user_department_entry.grid(row=24, column=25)
user_address_label = ttk.Label(menu_edit_window, text="详细地址:", width=10).grid(row=28, column=15)
user_address_entry = ttk.Entry(menu_edit_window, width=30)
user_address_entry.insert(0, edit_temp[8])
user_address_entry.grid(row=28, column=25)
# 修改窗口中创建一个修改功能按钮
edit_button = ttk.Button(menu_edit_window, text='修改')
edit_button.grid(row=32, column=25, pady=10)
edit_button.bind('', edit_button_click)
# 右键菜单设置
m = ttk.Menu(root, tearoff=0)
m.add_command(label="修改", command=click_menu_edit)
m.add_separator()
m.add_command(label="删除")
def do_popup(event):
try:
m.tk_popup(event.x_root, event.y_root)
finally:
m.grab_release()
tv.bind('', do_popup)
# 修改按钮功能函数
def edit_button_click(event):
MS_RET = Messagebox.yesno('是否决定修改?', title='警告', alert=False)
print(MS_RET)
if MS_RET == '确认':
print(edit_temp[1:9])
# 更改数据库中数据
edit_conn = sqlite3.connect('./datadb/test1.db')
edit_cur = edit_conn.cursor()
edit_sqlstr = f"update tab_person set name=?,sex=?,tel=?,birthday=?,police_id=?,person_id=?,department=?,address=? where ID = {edit_temp[0]}"
# print(edit_sqlstr)
try:
edit_cur.execute(edit_sqlstr, edit_temp[1:9])
print(edit_sqlstr)
# 递交事务
edit_conn.commit()
print('更改成功')
edit_conn.rollback()
except Exception as e:
print(e)
print('更改失败')
time.sleep(3)
edit_cur.close()
edit_conn.close()
menu_edit_window.destroy()
tv.bind('', treeviewClick)
root.mainloop()
创建数据库的代码
import sqlite3 # 导入sqlite3模块
# 创建连接对象
conn=sqlite3.connect('test1.db') #创建或打开数据库
# 创建游标对象
cur=conn.cursor() # 创建游标对象
# SQL语句
sql = '''create table tab_person(
ID INTEGER primary key autoincrement,
name VARCHAR not null,
sex VARCHAR not null,
tel VARCHAR not null,
birthday DATE not null,
police_id VARCHAR not null,
person_id VARCHAR not null,
department VARCHAR not null,
address VARCHAR not null
)'''
try:
# 提交事务
cur.execute(sql)
print('数据库创建成功')
except Exception as e:
print(e)
print('创建失败')
finally:
# 关闭游标对象
cur.close()
# 关闭连接对象
conn.close()
edit_sqlstr = f"update tab_person set name=?,sex=?,tel=?,birthday=?,police_id=?,person_id=?,department=?,address=? where ID = {edit_temp[0]}"
你这改了个寂寞。一堆问号 值呢 只是给了个ID