我正在尝试使用连接到数据库的Tkinter创建一个登录页。用户的详细信息应在注册后存储在数据库中,然后在登录时用于验证用户。注册部分工作正常,但是,登录验证过程中似乎有一个错误,因为即使输入了正确的凭据,程序也会不断输出“User Not Found”。 如果您能深入了解为什么会发生这种情况,我们将不胜感激
db_cursor = db_connection.cursor()
db_cursor.execute("CREATE DATABASE IF NOT EXISTS userdetails")
db_cursor.execute("SHOW DATABASES")
for db in db_cursor:
print(db)
db_cursor.execute("CREATE TABLE IF NOT EXISTS userdetails.users(userid INT(100)
AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255))")
db_cursor.execute("SHOW TABLES IN userdetails")
for table in db_cursor:
print(table)
class LoginPage:
def __init__(self):
self.userID = 1
self.mainpage()
def mainpage(self):
global mainpage
mainpage = Tk()
mainpage.geometry("600x600")
mainpage.title("Login Page")
mainpage.configure(background="thistle1")
Label(mainpage, text="Please login or register for a new account", bg="plum2", width="300", height="2", font=("Calibri", 13)).pack()
Button(mainpage, text="Login",bg="SkyBlue1", height="2", width="30", command=self.login).pack()
Button(mainpage, text="Register",bg="SkyBlue1", height="2", width="30", command=self.register).pack()
mainpage.mainloop()
def register(self):
global register
global username
global password
Label(mainpage, text="Fill in the boxes below to register as a new user", bg="plum2").pack()
username = str
username_label = Label(mainpage, text="Username: ", bg="SkyBlue1").pack()
usernameRegister_entry = Entry(mainpage, textvariable=username).pack()
password = str
password_label = Label(mainpage, text="Password: ",bg="SkyBlue1").pack()
passwordRegister_entry = Entry(mainpage, textvariable=password, show='*').pack()
Button(mainpage, text="Register", width=10, height=1, bg="plum2", command = self.new_user).pack()
def new_user(self):
self.userID += 1
userdetails_sql_query = "INSERT INTO userdetails.users VALUES(userID,'username','password')"
db_cursor.execute(userdetails_sql_query)
db_connection.commit()
print(db_cursor.rowcount, "Record Inserted")
Label(mainpage,text="Registration Success", bg="plum2", font=("calibri", 11)).pack()
def login(self):
global usernameCheck
global passwordCheck
global login
Label(mainpage, text="Fill in the boxes below to login", bg="plum2").pack()
usernameCheck = str
Label(mainpage, text="Username: ", bg="SkyBlue1").pack()
usernameLogin_entry = Entry(mainpage, textvariable=usernameCheck).pack()
passwordCheck = str
Label(mainpage, text="Password: ", bg="SkyBlue1").pack()
passwordLogin_entry = Entry(mainpage, textvariable=passwordCheck, show= '*').pack()
Button(mainpage, text="Login", width=10, height=1, bg="plum2", command=self.login_verification).pack()
def login_verification(self):
sql_select_Query = "select * from userdetails.users"
db_cursor.execute(sql_select_Query)
records = db_cursor.fetchall()
if usernameCheck in records:
if passwordCheck in records:
self.login_success()
else:
self.incorrect_password()
else:
self.incorrect_user()
def login_success(self):
global login_success
Label(mainpage, text="You have logged in successfully!", bg="plum2").pack()
return self.login_success
def incorrect_password(self):
Label(mainpage, text="Invalid Password ", bg="plum2").pack()
def incorrect_user(self):
Label(mainpage, text="User Not Found", bg="plum2").pack()
这不对
您可以迭代记录,并为每个记录检查用户和密码,但即使这样也不是这样做的方法
您应该构建一个参数化查询,检查用户和密码,然后检查查询是否返回记录
像这样的
相关问题 更多 >
编程相关推荐