Program cannot identify when username is correct sqlite3

50 Views Asked by At

I cannot get this login program to take the username and password fetched from database and compare it to entered one. It just says that the username is incorrect when it is not. Please help thanks

def loginpage():
    bottom= Toplevel()
    bottom.title("SmartCards - Login")
    bottom.iconbitmap(r"C:\Users\Ethan\Documents\Comp NEA\Icon.ico")
    bottom.config(padx=50, pady=50, bg=BACKGROUND_COLOR)
    
    def submit2():
        user1 = username1.get()
        passwrd1 = password1.get()
        sql = 'SELECT * FROM login WHERE "user1" == username'
        sql2 = "SELECT * FROM login WHERE 'passwrd1' == password"
        c.execute(sql)
        c.execute(sql2)
        conn.commit()
        if sql==user1:
            if sql2==passwrd1:
                messagebox.showinfo("Success","Login Successful!")
                
            else:
                messagebox.showwarning("Error","Password Incorrect!")
        
        else:
            messagebox.showwarning("Error","Username Incorrect!")
    
    userbox = Label (bottom, text="Username:")
    userbox.grid(row=0, column=0)
    username1 = Entry(bottom)
    username1.insert(0,"")
    username1.grid(row=1, column=0)
    passbox1 = Label (bottom, text="Password:")
    passbox1.grid(row=2, column=0)
    password1 = Entry(bottom)
    password1.insert(0,"")
    password1.grid(row=3, column=0)
    submit = Button(bottom, text="Submit", command=submit2)
    submit.grid(row=4, column=0)     
1

There are 1 best solutions below

0
John Gordon On

Use a single query for both username and password, with placeholder tokens instead of literal values:

sql = 'SELECT * FROM login WHERE username = %s AND password = %s'

Then execute the query, providing actual values for the placeholders:

c.execute(sql, [user1, password1])

Then fetch the results of the query and look at how many matching rows were found:

results = c.fetchall()
if len(results) == 0:
    # no matching users were found.  failure.
elif len(results) == 1:
    # exactly one matching user was found.  success!
else:
    # more than one user was found.  this is probably a failure?