Home > Software engineering >  SQL code is not comparing variables given by TKinter from within a function
SQL code is not comparing variables given by TKinter from within a function

Time:08-30

The program I'm currently working on should take two inputs given during a function and use them during another function to verify a user, this is done using Tkinter and SQL but the SQL either throws up an error or does not print or progress at all despite it being coded to do so, currently the result it should give is printing True when the password give and the result of the SQL match, I'm wondering if either the SQL is jankey or the way I've set up the variables is, either way I'm beating my head against trying to figure it out so any help would be appreciated, I'm fairly sure the problem is only with the submitlog fuction if that narrows down any issues.

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

previous attempts to rectify involve me trying to update the variables in both functions as well as running the login page outside of a function but all attempts resulted in either and an error or no activity at all

CodePudding user response:

Maybe this helps

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

Thanks Barmar for the helpful comments.

CodePudding user response:

There's no need to use a for loop. Assuming usernames are unique, the query either returns a row or it doesn't. So you can call cursor1.fetchone() to get that row, then check the password.

def submitlog(): #a function that will compare and entered username and password with present passwords on the login database
    cursor1 = connection1.cursor()
    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)
  • Related