[英]My simple login system using SQLite3 doesn't seem to get the matching ID/Password from the database
這是我在 Stackoverflow 上發表的第一篇文章。
我使用 SQLite3 為登錄系統編寫了一個簡單的代碼,當我在使用 Tkinter 制作的登錄表單中輸入正確的 ID 和密碼時,它沒有讓我通過,我只是不知道為什么。 我嘗試了 ChatGPT 告訴我做的所有事情,但它仍然不起作用。
大家可以幫我看看是什么問題嗎?
下面是我的代碼:
from tkinter import *
from tkinter import messagebox
import sqlite3
#------------------------------DB------------------------------------
#connect to the DB
con = sqlite3.connect('SeaSideDB.db')
#execute SQL statements and fetch results from SQL queries (the object 'con' represents the connection to the on-disk DB)
cur = con.cursor()
#create a table
cur.execute("CREATE TABLE IF NOT EXISTS login(ID,PASSWORD)")
#verifying that the new table's been created by querying the table
res = cur.execute("SELECT name FROM sqlite_master")
#add rows of data supplied as SQL literals by exercuting an INSERT statement
cur.execute("""
INSERT INTO login VALUES
('Juke',1234)
""")
#call this to commit the transaction
con.commit()
res = cur.execute("SELECT ID FROM login")
res.fetchall()
#---------------------------END OF DB------------------------------------
#--------------------------LOGIN GUI--------------------------------------
root = Tk(className = 'Seaside Loyalty Login')
root.geometry("500x150")
#ID input
L = Label(root, text = "ID")
T = Text(root, height = 1, width = 25)
L.config(font =("Courier",13))
L.pack()
T.pack()
#password input
L1 = Label(root, text = "Password")
T1 = Text(root, height = 1, width = 25)
L1.config(font =("Courier",13))
L1.pack()
T1.pack()
#----------------------------END OF LOGIN GUI-----------------------------------
#function that checks to see if the ID and PASSWORD match
def login(ID, PASSWORD):
cur = con.cursor()
cur.execute("SELECT * FROM login WHERE ID = ? AND PASSWORD = ?", (ID, PASSWORD))
result = cur.fetchone()
if result:
return True
else:
return False
#button function for credentials and it checks to see if the ID and PASSWORD entered by the user match the ones stored in DB.
def check_credentials():
ID = T.get("1.0", 'end-1c').strip()
PASSWORD = T1.get("1.0", 'end-1c').strip()
if login(ID, PASSWORD):
messagebox.showinfo("Login","Successful")
else:
messagebox.showerror("Login", "Invalid ID or Password")
#button input
B = Button(root, text = "Login", command = check_credentials)
B.place_configure(rely = 0.1)
B.pack()
root.mainloop()
#close DB after program exits
con.close()
先感謝您!
似乎數據庫設置正確,但我認為問題出在從數據庫中獲取表並將其與登錄表單上的輸入匹配。
T1.get()
返回一個字符串,但您在表的PASSWORD
列中存儲了一個 integer。 將此列更改為字符串:
cur.execute("""
INSERT INTO login VALUES
('Juke','1234')
""")
您還可以在CREATE TABLE
語句中指定列的數據類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.