我无法让这个登录程序获取从数据库中获取的用户名和密码并将其与输入的用户名和密码进行比较。它只是说用户名不正确,而事实并非如此。请帮忙谢谢
def loginpage():
bottom= Toplevel()
bottom.title("SmartCards - Login")
bottom.iconbitmap(r"C:\Users\Ethan\Documents\Comp NEA\Icon.ico")
bottom.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
def submit2():
user1 = username1.get()
passwrd1 = password1.get()
sql = 'SELECT * FROM login WHERE "user1" == username'
sql2 = "SELECT * FROM login WHERE 'passwrd1' == password"
c.execute(sql)
c.execute(sql2)
conn.commit()
if sql==user1:
if sql2==passwrd1:
messagebox.showinfo("Success","Login Successful!")
else:
messagebox.showwarning("Error","Password Incorrect!")
else:
messagebox.showwarning("Error","Username Incorrect!")
userbox = Label (bottom, text="Username:")
userbox.grid(row=0, column=0)
username1 = Entry(bottom)
username1.insert(0,"")
username1.grid(row=1, column=0)
passbox1 = Label (bottom, text="Password:")
passbox1.grid(row=2, column=0)
password1 = Entry(bottom)
password1.insert(0,"")
password1.grid(row=3, column=0)
submit = Button(bottom, text="Submit", command=submit2)
submit.grid(row=4, column=0)
对用户名和密码使用单个查询,并使用占位符而不是文字值:
sql = 'SELECT * FROM login WHERE username = %s AND password = %s'
然后执行查询,用实际的用户名和密码值填充占位符:
c.execute(sql, [user1, password1])
然后获取查询结果并查看找到了多少匹配行。
results = c.fetchall()
if len(results) == 0:
# no matching users were found. failure.
elif len(results) == 1:
# exactly one matching user was found. success!
else:
# more than one user was found. this is probably a failure?