[英]Using an SQLite return as a password field within Bcrypt (Python)
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.我想检查输入的密码是否与数据库中存储的密码相同,但是当我使用
bcrypt.checkpw()
时,它返回一个错误,指出它需要一个字符串或字节,因为 SQL 查询返回一个元组。 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
Simply adding row[0] within the function solves the problem as it returns the first (and only) value inside the tuple.只需在 function 中添加 row[0] 即可解决问题,因为它会返回元组中的第一个(也是唯一一个)值。
Treat it like a list in other words换句话说,把它当作一个列表
Extracting the a value from a tuple when the other values are unused 当其他值未使用时从元组中提取一个值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.