我正在使用 tkiner 制作一个密码库 GUI 应用程序。我正在尝试添加一个功能,让我可以编辑和更新 gui 中的任何特定行。我的意思类似于remove_entry函数如何删除它所在的行,我希望edit_password函数编辑它所在的任何行。此外,我想要 save_new_password 函数来更新数据库中的保管库表。
以下是我所指的功能:
def remove_entry(input):
cursor.execute("DELETE FROM vault WHERE id = ?", (input,))
db.commit()
password_vault()
def edit_password():
text1.config(state="normal")
text2.config(state="normal")
text3.config(state="normal")
text4.config(state="normal")
def save_new_password():
website = str(text1.get("1.0", "end"))
username = str(text2.get("1.0", "end"))
password = str(text3.get("1.0", "end"))
day = str(text4.get("1.0", "end"))
insert_fields = """UPDATE vault (website,username,password, day)
VALUES(?, ?, ?, ?)"""
cursor.execute(insert_fields, [website, username, password, day])
db.commit()
text1.config(state="disabled")
text2.config(state="disabled")
text3.config(state="disabled")
text4.config(state="disabled")
password_vault()
这是我的参考代码:
import sqlite3
import hashlib
from tkinter import *
import tkinter as tk
from functools import partial
from tkinter import simpledialog
with sqlite3.connect("password_vault.db") as db:
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS master(
id INTEGER PRIMARY KEY,
password TEXT NOT NULL);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS vault(
id INTEGER PRIMARY KEY,
website TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL,
day TEXT NOT NULL);
""")
def popup(text):
answer = simpledialog.askstring("input string", text)
return answer
window = Tk()
window.title("Password Manager")
def hashed_master_password(input):
hash = hashlib.sha512(input).hexdigest()
return hash
def setup():
window.geometry("400x400")
setup_password = tk.Label(window, text="Create master password")
setup_password.config(anchor="e")
setup_password.pack()
create_password = tk.Entry(window, width=20)
create_password.focus()
create_password.pack()
setup_label = tk.Label(window, text="Re-enter password")
setup_label.pack()
confirm_password = tk.Entry(window, width=20)
confirm_password.pack()
fail_to_match = tk.Label(window)
fail_to_match.pack()
def save_password():
if create_password.get() == confirm_password.get():
hashed_password = hashed_master_password(create_password.get().encode("UTF-8"))
insert_password = """INSERT INTO master(password) VALUES(?)"""
cursor.execute(insert_password, [hashed_password])
db.commit()
password_vault()
else:
fail_to_match.config(text="Passwords do not match")
setup_button = tk.Button(window, text="set password", command=save_password)
setup_button.pack()
def login_screen():
window.geometry("500x500")
master_password = tk.Label(window, text="Enter Master Password")
master_password.config(anchor="e")
master_password.pack()
enter_password = tk.Entry(window, width=20, show="*")
enter_password.focus()
enter_password.pack()
fail_label = tk.Label(window)
fail_label.pack()
def get_master_password():
check_hash_password = hashed_master_password(enter_password.get().encode("UTF-8"))
cursor.execute("SELECT * FROM master WHERE id=1 AND password = ?", [check_hash_password])
return cursor.fetchall()
def check_password():
match = get_master_password()
if match:
password_vault()
else:
enter_password.delete(0, "end")
fail_label.config(text="Wrong Password")
pass_check_button = tk.Button(window, text="Submit", pady=5, command=check_password)
pass_check_button.pack()
def password_vault():
for widget in window.winfo_children():
widget.destroy()
def add_entry():
website1 = "website"
username1 = "username"
password1 = "password"
days1 = "days password is good for"
website = popup(website1)
username = popup(username1)
password = popup(password1)
day = popup(days1)
insert_fields = """INSERT INTO vault(website,username,password, day)
VALUES(?, ?, ?, ?)"""
cursor.execute(insert_fields, (website, username, password, day))
db.commit()
password_vault()
def remove_entry(input):
cursor.execute("DELETE FROM vault WHERE id = ?", (input,))
db.commit()
password_vault()
window.geometry("1550x700")
main_frame = Frame(window)
main_frame.grid(sticky="news")
my_canvas = Canvas(main_frame, height=700, width=1525)
my_canvas.grid(row=0, column=9, sticky='news')
my_canvas.grid_rowconfigure(0, weight=10)
my_frame = Frame(my_canvas, bd=1, relief="solid")
my_canvas.create_window(0, 0, window=my_frame, anchor="nw")
scrollbar = Scrollbar(main_frame, command=my_canvas.yview)
scrollbar.grid(row=0, column=10, sticky="ns")
my_canvas.configure(yscrollcommand=scrollbar.set)
my_frame.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
def edit_password():
text1.config(state="normal")
text2.config(state="normal")
text3.config(state="normal")
text4.config(state="normal")
vault_label = tk.Label(my_frame, text="Welcome to your password vault", font=("Time New Roman", 20))
vault_label.grid(column=1, row=0)
add_button = tk.Button(my_frame, text="Add new info", command=add_entry)
add_button.grid(column=0, padx=8)
website_label = tk.Label(my_frame, text="Website")
website_label.grid(row=2, column=0, padx=80)
username_label = tk.Label(my_frame, text="Username")
username_label.grid(row=2, column=1, padx=80, pady=8)
password_label = tk.Label(my_frame, text="Password")
password_label.grid(row=2, column=2, padx=80)
days_password_is_good_for = tk.Label(my_frame, text="Days password is good for")
days_password_is_good_for.grid(row=2, column=3, padx=80)
cursor.execute("SELECT * FROM vault")
if cursor.fetchall() is not None:
i = 0
while True:
cursor.execute("SELECT * FROM vault")
array = cursor.fetchall()
text1 = tk.Text(my_frame)
text1.insert("1.0", array[i][1])
text1.config(height=1, width=20, state="disabled")
text1.grid(column=0, row=i+4)
text2 = tk.Text(my_frame)
text2.insert("1.0", array[i][2])
text2.config(height=1, width=20, state="disabled")
text2.grid(column=1, row=i+4)
text3 = tk.Text(my_frame)
text3.insert("1.0", array[i][3])
text3.config(height=1, width=20, state="disabled")
text3.grid(column=2, row=i+4)
text4 = tk.Text(my_frame)
text4.insert("1.0", array[i][4])
text4.config(height=1, width=20, state="disabled")
text4.grid(column=3, row=i+4)
delete_button = tk.Button(my_frame, text="Delete", command=partial(remove_entry, array[i][0]))
delete_button.grid(column=4, row=i+4, pady=8, padx=10)
edit_button = tk.Button(my_frame, text="Update information", command=edit_password)
edit_button.grid(column=5, row=i+4, pady=8, padx=10)
def save_new_password():
website = str(text1.get("1.0", "end"))
username = str(text2.get("1.0", "end"))
password = str(text3.get("1.0", "end"))
day = str(text4.get("1.0", "end"))
insert_fields = """UPDATE vault (website,username,password, day)
VALUES(?, ?, ?, ?)"""
cursor.execute(insert_fields, [website, username, password, day])
db.commit()
text1.config(state="disabled")
text2.config(state="disabled")
text3.config(state="disabled")
text4.config(state="disabled")
password_vault()
password_save_button = tk.Button(my_frame, text="Save your updated information",
command=save_new_password)
password_save_button.grid(column=6, row=i+4, pady=8, padx=10)
i = i+1
cursor.execute("SELECT * FROM VAULT")
if (len(cursor.fetchall()) <= i):
break
cursor.execute("SELECT * FROM master")
if cursor.fetchall():
login_screen()
else:
setup()
window.mainloop()
主要问题是 edit_password 只允许我编辑最后一行,而 save_new_password 给了我这个错误
cursor.execute(insert_fields, [网站、用户名、密码、日期]) sqlite3.OperationalError:靠近“(”:语法错误
您可能混合了插入和更新的语法。在 sqlite 上发出更新查询时,您需要指定语句的
SET
部分,详细信息如下:https://www.sqlite.org/lang_update.html
您的查询是:
UPDATE vault SET website = ?, username = ?, password = ?, day = ? WHERE [...]
使用
WHERE
子句检索要更新的行。
编辑:这是您应该修改第 204:220 行部分的方式。
def save_new_password(input):
website = str(text1.get("1.0", "end"))
username = str(text2.get("1.0", "end"))
password = str(text3.get("1.0", "end"))
day = str(text4.get("1.0", "end"))
insert_fields = """UPDATE vault SET website = ?, username = ?, password = ?, day = ? WHERE id = ?"""
cursor.execute(insert_fields, [website, username, password, day, input])
db.commit()
text1.config(state="disabled")
text2.config(state="disabled")
text3.config(state="disabled")
text4.config(state="disabled")
password_vault()
password_save_button = tk.Button(my_frame, text="Save your updated information",
command=partial(save_new_password, array[i][0]))
password_save_button.grid(column=6, row=i+4, pady=8, padx=10)