如何检查输入的值是否等于SQL数据库中的记录?

huangapple go评论54阅读模式
英文:

how to check if the value of entry is equal to a record in sql database

问题

以下是代码的翻译部分:

def login_submit():
    n1 = en_username.get()
    n2 = en_password.get()
    q1 = "select password from data where username = '{}';".format(n1)
    cursor.execute(q1)
    r = cursor.fetchone()
    if r:
        if r == n2:
            success = messagebox.showinfo(title="Logged in", message="Logged in Succesfully")
        elif r != n2:
            wrong_pas = messagebox.showerror(title="Failed to login", message="Wrong password")
    else:
        fail = messagebox.showerror(title="Error", message="No such account exists")
        firstwindow()

def login():
    global login_win
    global en_username
    global en_password
    firstwin.destroy()
    login_win= Toplevel()
    login_win.title("Login")
    icon= ImageTk.PhotoImage(file= "C://Users//Win10//photos//icon.jpg")
    login_win.iconphoto(False, icon)
    login_win.geometry("300x140")
    
    login_username = Label(login_win, text="Username: ", anchor=W, font=("Arial", 12))
    login_username.pack(anchor=W, ipady= 10)
    login_passwd = Label(login_win, text="Password: ", anchor=W, font=("Arial", 12))
    login_passwd.pack(anchor=W, ipady=10)
    submit_btn = Button(login_win, text="Submit", command= login_submit).pack(anchor=S)
    goback = Button(login_win,text="Go Back", command= go_lback).pack(anchor=S)

    en_username = Entry(login_win, bd=3, relief=GROOVE, font=("Arial", 9))
    en_username.place(x= 90, y= 10)
    en_password = Entry(login_win, bd=3, relief=GROOVE, font=("Arial", 9), show="*")
    en_password.place(x= 90, y= 55)

请注意,代码中的特殊字符(如")已经被恢复为原始的Python代码。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

I made this simple gui where it asks u to sing up or sign in. when we sign up our details we put in the entry are stored in an sql database. when u login it takes the value of username entry and checks if that username exists in the sql table. if it exists it checks if the password we entered matches to the corresponding username.

def login_submit():
    n1 = en_username.get()
    n2 = en_password.get()
    q1 = "select password from data where username = '{}';".format(n1)
    cursor.execute(q1)
    r = cursor.fetchone()
    if r:
        if r == n2:
            success = messagebox.showinfo(title="Logged in", message="Logged in Succesfully")
        elif r != n2:
            wrong_pas = messagebox.showerror(title="Failed to login", message="Wrong password")
    else:
        fail = messagebox.showerror(title="Error", message="No such account exists")
        firstwindow()



def login():
    global login_win
    global en_username
    global en_password
    firstwin.destroy()
    login_win= Toplevel()
    login_win.title("Login")
    icon= ImageTk.PhotoImage(file= "C://Users//Win10//photos//icon.jpg")
    login_win.iconphoto(False, icon)
    login_win.geometry("300x140")
    
    login_username = Label(login_win, text="Username: ", anchor=W, font=("Arial", 12))
    login_username.pack(anchor=W, ipady= 10)
    login_passwd = Label(login_win, text="Password: ", anchor=W, font=("Arial", 12))
    login_passwd.pack(anchor=W, ipady=10)
    submit_btn = Button(login_win, text="Submit", command= login_submit).pack(anchor=S)
    goback = Button(login_win,text="Go Back", command= go_lback).pack(anchor=S)

    en_username = Entry(login_win, bd=3, relief=GROOVE, font=("Arial", 9))
    en_username.place(x= 90, y= 10)
    en_password = Entry(login_win, bd=3, relief=GROOVE, font=("Arial", 9), show="*")
    en_password.place(x= 90, y= 55)

But whenever i trying logging in the incorrect password message box pops up even if i put the right password.

答案1

得分: 1

你正在将密码值(n2)与从数据库检索的元组(r)进行比较,这始终会计算为False,因此每次都会失败。

以下是您可以修复此问题的方法:

def login_submit():
    n1 = en_username.get()
    n2 = en_password.get()
    q1 = "select password from data where username = '{}';".format(n1)
    cursor.execute(q1)
    r = cursor.fetchone()
    if r:
        if r[0] == n2:  # 将密码与元组的第一个元素进行比较
            success = messagebox.showinfo(title="登录成功", message="登录成功")
        else:
            wrong_pas = messagebox.showerror(title="登录失败", message="密码错误")
    else:
        fail = messagebox.showerror(title="错误", message="没有这样的账户存在")
        firstwindow()

注意:这是对您提供的代码的翻译,不包括代码部分。

英文:

You are comparing the password value (n2) with a tuple (r) fetched from the database, which always evaluates to False, so this fail each times.

Here is how you could fix that:

def login_submit():
    n1 = en_username.get()
    n2 = en_password.get()
    q1 = "select password from data where username = '{}';".format(n1)
    cursor.execute(q1)
    r = cursor.fetchone()
    if r:
        if r[0] == n2:  # Compare the password with the first element of the tuple
            success = messagebox.showinfo(title="Logged in", message="Logged in Successfully")
        else:
            wrong_pas = messagebox.showerror(title="Failed to login", message="Wrong password")
    else:
        fail = messagebox.showerror(title="Error", message="No such account exists")
        firstwindow()

huangapple
  • 本文由 发表于 2023年4月4日 15:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定