这是我在Stackoverflow上的第一篇文章。
我写了一个简单的代码为登录系统使用SQLite3,当我输入正确的ID和密码与Tkinter的登录表单,它不让我通过,我只是不知道为什么。我试了所有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()
提前感谢!
似乎DB设置正确,但我认为问题来自于从DB获取表并将其与登录表单上的输入相匹配。
T1.get()
返回一个字符串,但是您在表的PASSWORD
列中存储了一个整数。将此列更改为字符串:
cur.execute("""
INSERT INTO login VALUES
('Juke','1234')
""")
您还可以在CREATE TABLE
语句中指定列的数据类型。