Home > Software design >  How can i compare a value from database to python input
How can i compare a value from database to python input

Time:09-23

So im trying to create a login system kinda, normal python terminal, and i made a register function but im struggling with the login one, im trying to compare my input to the username and password and when i get that done ill add the ids but how can i do that, i tried everything

when i run the code and enter the right details, it is telling me "Login failed, wrong username or password", which means that something is wrong with my if statement

    import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password=""
)
mycursor = mydb.cursor(buffered=True)

def login():
    mycursor.execute("USE logintest")
    login_username = input("Please enter your username or email: ")
    login_password = input("Please enter your password: ")
    
    check_login = f"SELECT username FROM users WHERE username = '{login_username}'"
    check_password = f"SELECT password FROM users WHERE password = '{login_password}'"

    mycursor.execute(check_login)
    username_result = mycursor.fetchall()

    mycursor.execute(check_password)
    password_result = mycursor.fetchall()

    passwordr = password_result
    usernamer = username_result

    print(usernamer)
    print(passwordr)

    if login_password == passwordr and login_username == usernamer:
        print("Logged in successfully")
    else: 
        print("Login failed, wrong username or password")
        
        
    
def register():
    mycursor.execute("USE logintest")
    new_username = input("please pick a username: ")
    new_email = input("please enter your email: ")
    new_password = input("please pick a password: ")
    insert_new_user = "INSERT INTO users (username, email, password) VALUES (%s, %s, %s)"
    new_user = (new_username, new_email, new_password)
    mycursor.execute(insert_new_user, new_user)
    mydb.commit()
    print("User successfully created! insert id:", mycursor.lastrowid)
    

def options():
    print("1. login")
    print("2. register")
    options = input("please pick 1 or 2: ")
    if "1" in options:
        login()
    elif "2" in options:
        register()
    else: 
        print("please only select 1 or 2")
        options()

options()

CodePudding user response:

Usually when you fetch data from a database in python, it returns a list of the data, and input in python is a string, so in other words, you are comparing a string with a list which will always be false.

CodePudding user response:

you should create a function for verifying the login details. This is how to verify the login details, it should be inside the function:

         try:
        username = input("your username")
        password = input("your password")

        conn = (your connection code);
        result = conn.execute("SELECT * FROM yourTable WHERE usernameColumnOfYourTable = ? AND passwordColumnOFyourTable = ?", (username, password))
        print("connected to database")
        if (len(result.fetchall()) > 0):
            print("user found")

        else:
            print("user not found")

    except Exception as err:
        print("couldn't connect")
        print("General error :: ", err)

Note: ? is the parameter marker for pyodbc module, if you're using mysql connector, replace the ? sign with %s

But if you're using a hashed (salted) password, the format will be a bit different.

Applying this logic to your codes:

 def login():
connection = (establish your database connection here)
login_username = input("Please enter your username or email: ")
login_password = input("Please enter your password: ")

result = connection.execute("SELECT username FROM users WHERE username = ? AND password = ?",(login_username, login_password) )"

if (len(result.fetchall()) > 0):

    print("Logged in successfully")
else: 
    print("Login failed, wrong username or password")
    
    
  • Related