Python 检查用户';在输入框中输入,以查看它是否在SQLite数据库中
任何问题请告诉我。再次感谢。说明:Python 检查用户';在输入框中输入,以查看它是否在SQLite数据库中,python,tkinter,Python,Tkinter,任何问题请告诉我。再次感谢。说明: 记录的示例如下所示: database.commit() database.close() root.mainloop() tk.mainloop() 因此,当您循环浏览此列表时,您的每个项都将是一个元组 [(1,2,3,4),(a,b,c,d)] 意思是你的rcrd或rcrd是一个元组。您正在以下位置将元组与str进行比较: >>>for rcrd in records: ... rcrd
记录的示例如下所示:
database.commit()
database.close()
root.mainloop()
tk.mainloop()
因此,当您循环浏览此列表时,您的每个项都将是一个元组
[(1,2,3,4),(a,b,c,d)]
意思是你的rcrd
或rcrd
是一个元组。您正在以下位置将元组
与str
进行比较:
>>>for rcrd in records:
... rcrded = rcrd
(1,2,3,4) # This is rcrded
(a,b,c,d)
因此,前两个if
始终为真,因为字符串和元组从不相同
解决方案:
可能的解决方案是列出用户名和密码,然后使用用户输入进行检查,如:
if rcrded != entry_1.get():
unrecognisable_un()
if rcrded != entry_2.get():
unrecognisable_pw()
if rcrded == entry_1.get() and entry_2.get():
successful_login()
其他错误:
还要注意,您的if
语句也是错误的。当你说:
username = []
passw = []
for rcrd in records:
username.append(rcrd[1]) # Make a list of all the usernames
passw.append(rcrd[2]) # Make a list of all passwords
# If statement to compare whether the user entered the correct username and password:
if entry_1.get() in username: # If username exists inside the list
idx = username.index(entry_1.get()) # Get its index
if entry_2.get() == passw[idx]: # If password is the corresponding value in other list
successful_login() # Success
else:
unrecognisable_pw() # Fail
else: # If username not there in list, then
unrecognisable_un()
这与:
if rcrded == entry_1.get() and entry_2.get():
因此它检查entry_2.get()
是否为True
,所有非空字符串是否为True
。这不是你想在这里使用的
或者:
其他可能的简单解决方案是将SQL查询更改为:
if rcrded == entry_1.get() and entry_2.get() == True:
尽管这无法识别用户名或密码是否无效。使用print()
验证您输入的内容是否符合您的要求。解决这一问题的最佳方法是自己动手,因为在您尝试print(record,rcrded)
之后,您会意识到为什么if
语句失败。难道print(record,rcrded)
不会在python控制台中打印实际结果,而不是在单独的tkinter窗口中显示消息吗?是的,您可以使用在条目小部件中键入的内容来验证结果。你能在问题中包括打印(记录)
的输出吗?好的,那么在我的代码中打印(记录,rcrded)
到哪里去?在if
之前。好的,所以我只是在def login\u page():
函数的records=c.fetchall()下复制并粘贴了你的解决方案。那是我应该放的地方吗?如果是这样,尽管在数据库中输入了正确的用户名和密码,但显示消息“your usernmae/password is Error”(您的用户名/密码不正确)的单独tkinter窗口仍会出现。现在检查更新的代码。如果rcrded==entry_1.get()和entry_2.get(),则不确定在哪里==True:
将继续,但我复制并粘贴了更新代码,不幸的是没有任何区别。它对你有用吗?你不应该用它。那是你的错误。只需使用给出的解决方案。没问题,就可以了。
c.execute("SELECT * FROM Credentials where username=%s and password=%s",(entry_1.get(),entry_2.get()))
records = c.fetchall()
if records != []: # If matching result exists, then success
successful_login()
else:
unrecognisable_un() # Fail