Sorry if this seems like a stupid question, but I am trying to use a variable from a method in one of my classes. Its a simple login system, with no real emphasis on security, all im trying to do is make it so that once the username is confirmed to be in the database, it ensures that it searches only the row with that username to see if the password matches. In the following simplified example, I want to be able to use the confirmedUser variable to assist me in an SQL search.
class loginUsername(root) def advance(): if usernameFound == True: confirmedUser = x #open new window class loginPassword(root) def advance(): mycursor.execute("SELECT password FROM staff_logins WHERE username = '"+ confirmedUser +"'") if passwordFound == True: #opens new window
I apologise if this is hard to understand, I tried simplying it and might’ve messed up, but what im trying to do is use that variable in my SQL querey to check if the password matches the username. Please feel free to ask if I havent made it clear I’d appreciate any help from others.
First of all why are you craeting a class with inside only a function? Maybe you are new to python but you don’t need to create a class to define a function, just define the function itself. Second in python, as in all the other languages that I know, when you create a function, all the variables that will be created or modified inside of it will not be saved anywere, if you want to save them somewhere you have a couple of choices. The first is to simply save them iside of a global variable(that doesn’t mean only int, float or string):
confirmedUser = "" #could be also an array of string or whatever you want def advance(): if usernameFound == True: global confirmedUser confirmedUser = x def advance(): confirmedUser mycursor.execute("SELECT password FROM staff_logins WHERE username = '"+ confirmedUser +"'") if passwordFound == True: #your stuff
another way is, when you call a function, equals it to a variable, that will obviously need a return statement:
def loginUsername(usernameFound): if usernameFound == True: confirmedUser = x return confirmedUser def loginPassword(confirmedUser): mycursor.execute("SELECT password FROM staff_logins WHERE username = '"+ confirmedUser +"'") if passwordFound == True: #your stuff confirmedUser = loginUsername() loginPassword(confirmedUser)
In this case if “usernameFound” will be False it will return nothing, so confirmedUser will be a null value. I hope I answered at you question, and forgive me for my poor english, but I’m not a native speaker. Last thing: the code that you shared with us is pretty incomplete so it is hard to comprehend what you are doing in it, next time, if possible, please, share the entire code or at least all the code needed to correctly undestand what you are doing, it’s better to put unnecessary code than don’t put necessary code(it is not a reproach, just a suggestion).