Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading