0

I want to check if the input password is the same as that stored in the database but when I use bcrypt.checkpw() it returns an error saying that it expects a string or byte because the SQL query returns a tuple. I can't find a way to convert the database response to a byte from a tuple to make it compatible.

sql = ''' SELECT password FROM user_data WHERE username=? '''

username = input('Input username: ')
password = bytes(input('Input Password: '), encoding='utf-8')

cur = conn.cursor()
cur.execute(sql, (username,))
rows = cur.fetchall()

for row in rows:

    if bcrypt.checkpw(password, row):
        details = (user_id, username, password)
        print('logged in')

        return details

        break
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

-1

Simply adding row[0] within the function solves the problem as it returns the first (and only) value inside the tuple.

Treat it like a list in other words

Extracting the a value from a tuple when the other values are unused

  • Welcome to Stack Overflow! Please take the [tour] and read [answer]. Link-only answers are bad! You should summarize the linked page in your answer here so your answer doesn't become useless in case of [link-rot](https://en.wikipedia.org/wiki/Link_rot). [Why is linking bad?](//meta.stackexchange.com/q/7515/174780) | [Are answers that just contain links elsewhere really “good answers”?](//meta.stackexchange.com/q/8231/174780) | [Your answer is in another castle: when is an answer not an answer?](//meta.stackexchange.com/q/225370/174780) – Pranav Hosangadi Oct 26 '20 at 20:06
  • If this question is an exact duplicate, please close it as a duplicate instead of adding answers here. – Pranav Hosangadi Oct 26 '20 at 20:06
  • @PranavHosangadi it is not a duplicate it just is solvable with an answer to another question, I'm guessing that why you have a problem with it – Jenson Bolton Nov 11 '20 at 19:06