sqlite SQL代码不会比较函数内由TKinter给定的变量

8iwquhpp  于 2022-11-15  发布在  SQLite
关注(0)|答案(2)|浏览(139)

我目前正在开发的程序应该接受一个函数期间给出的两个输入,并在另一个函数中使用它们来验证用户,这是使用Tkinter和SQL完成的,但是SQL要么抛出错误,要么根本不打印或根本不前进,尽管它被编码这样做,目前它应该给出的结果是打印True当密码给出和SQL匹配的结果时,我想知道是不是SQL是jankey,或者我设置变量的方式是,无论是哪种方式,我都在绞尽脑汁试图弄清楚,这样任何帮助都会受到感谢。我相当肯定,如果提交日志函数缩小了任何问题的范围,那么问题只出在提交日志函数上。

import sqlite3  #necessary to use databse searching
import os    #necessary to check the date listed by the system
from tkinter import *    #necessary to use the GUI design features
from tkinter import messagebox #needed for message boxes

login = False #needed to start the login procedure
root =Tk() #defines the foundations for a created page
connection1 = sqlite3.connect("Login.db")   #code to read the database and allow crossreferencing of passwords
#connection2

Username = ""
Password = ""

def submitlog(): #a function that will compare and entered username and password with present passwords on the login database
    cursor1 = connection1.cursor()
    for row in cursor1.execute('SELECT Password FROM Login WHERE Username = "{}"'.format(Username.replace('"','""')), (Username))[0]:
        if row  == Password:
            print(True)
        else:
            print(Username)
            print(Password)
            print(row) 

def logOut():
    root.destroy
    exit()

def loginpage(): #defines the creation of a login page
    root.geometry("1000x700")
    root.resizable(False, False)
    root.configure(background = "Black")
    #creation of text frames
    frame_heading = Frame(root)
    frame_heading.grid(row=0, column=0, columnspan=2, padx=30, pady=5)
    #creation of entry frames
    frame_entry = Frame(root)
    frame_entry.grid(row=1, column=0, columnspan=2, padx=10, pady=10)
    Label(frame_heading, text="Please log in", font=("Arial", 16)).grid(row=0,column=0,padx=0,pady=5) #heading label
    #entry labels
    Label(frame_entry, text="Username: ").grid(row=0,column=0,padx=10,pady=10)
    Label(frame_entry, text="Password: ").grid(row=1,column=0,padx=10,pady=10)
    #text entry fields
    
    UserName = Entry(frame_entry,width=30, bg = "white")
    UserName.grid(row=0, column=1, padx=5, pady=5)
    PassWord = Entry(frame_entry,width=30, bg = "white")
    PassWord.grid(row=1, column=1, padx=5, pady=5)
    #placing the submit and log off buttons
    submit_button = Button(root, text = "Submit", width =10, command= submitlog)
    submit_button.grid(row=2,column=0,padx=0,pady=5)
    logout_button = Button(root, text = "Log out", width = 10, command = logOut)
    logout_button.grid(row=2,column=1,padx=0,pady=5)
    root.mainloop()

    Username = UserName.get()
    Password = PassWord.get()
    return Username and Password

以前的纠正尝试包括尝试更新两个函数中的变量,以及在函数外部运行登录页面,但所有尝试都会导致错误或根本没有活动

laik7k3q

laik7k3q1#

不需要使用for循环。假设用户名是唯一的,查询要么返回一行,要么不返回。因此,您可以调用cursor1.fetchone()来获取该行,然后检查密码。
您还需要在调用submitlog()时从输入字段中获取用户名和密码,而不是在第一次创建它们时。

def submitlog(): #a function that will compare and entered username and password with present passwords on the login database
    cursor1 = connection1.cursor()
    Username = UserName.get()
    Password = PassWord.get()
    cursor1.execute('SELECT Password FROM Login WHERE Username = ?', (Username,))
    row = cursor1.fetchone()
    if row and row[0] == Password:
        print(True)
    else:
        print(Username)
        print(Password)
        print(row)

您需要将global UserName, PassWord添加到loginpage(),以便可以从submitlog()访问变量。
顺便说一句,return Username and Password不是从函数返回多个值的方式。应该是return Username, Password。但我怀疑是否使用了返回值,因此您不需要返回任何内容。

v64noz0r

v64noz0r2#

也许这会有帮助

query = 'SELECT Password FROM Login WHERE Username = ?'
cursor.execute(query, (Username,))
results = cursor.fetchall()
for row in results:
    if row[0] == Password:
        print(True)

感谢Barmar的有益评论。

相关问题