如何检查表中是否不存在用户名?

问题描述 投票:0回答:1

我正在编写一个程序,该程序将用户输入作为用户名,然后查询数据库以检查用户名是否尚未使用。这是我到目前为止提出的代码,但是问题是,即使用户名确实存在于表中,它也始终返回“ None”作为对查询的响应。

mycursor= mydb.cursor()
username=input('username')
checkUsername= mycursor.execute('SELECT username FROM users WHERE username = %(username)s', {'username' :username})
print (checkUsername)
python mysql database validation username
1个回答
1
投票

[执行时,您只是告诉游标在数据库上传达该语句。它返回None,但它的作用是允许光标处于可以获取结果的状态。

mycursor = mydb.cursor()
username = input('username')
checkUsername = mycursor.execute('SELECT username FROM users WHERE username = %(username)s', {'username' :username})

row = cursor.fetchone()

username_exists = row is not None
© www.soinside.com 2019 - 2024. All rights reserved.