我有一個密碼系統,它將一個python程序的密碼存儲在SQL表中。我希望用戶能夠在tkinter窗口中更改密碼,但我不確定如何使用python變量的值作爲SQL表的值。下面是一個示例代碼:將python變量值插入到SQL表中
import tkinter
from tkinter import *
import sqlite3
conn = sqlite3.connect('testDataBase')
c = conn.cursor()
c.execute("INSERT INTO info Values('test')")
c.execute("SELECT password FROM info")
password = (c.fetchone()[0])
#Window setup
admin = tkinter.Tk()
admin.minsize(width=800, height = 600)
admin.maxsize(width=800, height = 600)
#GUI
passwordChangeLabel = Label(admin, text="It is recommended to change your password after first login!", font=("Arial", 14))
passwordChangeLabel.pack()
passwordChangeCurrentPasswordLabel = Label(admin, text="Current Password: ", font=("Arial", 11))
passwordChangeCurrentPasswordLabel.place(x=275, y=30)
passwordChangeCurrentPasswordEntry = Entry(admin)
passwordChangeCurrentPasswordEntry.place(x=405, y=32.5)
passwordChangeNewPasswordLabel = Label(admin, text="New Password: ", font=("Arial", 11))
passwordChangeNewPasswordLabel.place(x=295, y=50)
passwordChangeNewPasswordEntry = Entry(admin)
passwordChangeNewPasswordEntry.place(x=405, y=52.5)
passwordChangeButton = Button(admin, text="Submit", width=20)
passwordChangeButton.place(x=350, y=80)
def changePasswordFunction(event):
newPassword = passwordChangeNewPasswordEntry.get()
enteredPassword = passwordChangeCurrentPasswordEntry.get()
if enteredPassword == password:
c.execute("REPLACE INTO info(password) VALUES(newPassword)")
else:
wrong = tkinter.Toplevel()
wrong.minsize(width=200, height = 100)
wrong.maxsize(width=200, height = 100)
Label(wrong, text="Sorry that password is incorrect!", font=("Arial", 24), anchor=W, wraplength=180, fg="red").pack()
admin.bind('<Return>', changePasswordFunction)
passwordChangeButton.bind('<Button-1>', changePasswordFunction)
此代碼將彈出一個錯誤:
sqlite3.OperationalError: no such column: newPassword
我怎樣才能正確地與新輸入的密碼替換密碼列於前值?
您是否閱讀過有關參數化查詢的文檔? https://docs.python.org/2/library/sqlite3.html –