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

Question:

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.

Asked By: Krishan Kumar

||

Answers:

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()
Answered By: Saxtheowl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.