Home > front end >  How to get a password from SQlite database to compare to a user entered password in Python
How to get a password from SQlite database to compare to a user entered password in Python

Time:06-18

I am writing a small python script as a learning project (very new to writing code) that request a user to input login information and compares it with a connected SQlite database but I cant seem to get the queried password to properly work with the 'if password == pin:' part. I get 'Password Incorrect' even if the correct one has been entered.

If the password is 1234 and I print(pin) I get the correct password but it returns as (1234,). Entering the password as (1234,) doesnt work either.

I have tried to find a resoution but I'm not sure that I am asking the right question. Any guidance would be grealty appreciated.

import sqlite3

#Connects to database and creates object
connection = sqlite3.connect('/path/to/db')
cursor = connection.cursor()

#Selects the password from the dabase that corresponds to the name input.
name = input('Enter your name: ')
cursor.execute("SELECT password FROM users WHERE name = ?",(name,))
pin=cursor.fetchone()

#Compares the user entered password with the selected password from the database and should exit if a match.
while True:
    password = input('Enter Your Password:  ')
    if password == pin:
        print('Welcome '   (name))
        break
    else:
        print('Password Incorrect.')

CodePudding user response:

Thank you aj7amigo and Alberto Hanna! Both solutiuons were needed to fix this.

I needed to change password input to int(input('Enter your password: ') to convert string input to interger.

I also needed to change the comparison to if password == pin[0]: to get just the interger "1234" from the database.

Thanks again!

  • Related