我正在编写一个程序,该程序将用户输入作为用户名,然后查询数据库以检查用户名是否已被使用。这是我到目前为止提出的代码,但问题是它总是返回“None”作为对查询的响应,即使用户名确实存在于表中。
mycursor= mydb.cursor()
username=input('username')
checkUsername= mycursor.execute('SELECT username FROM users WHERE username = %(username)s', {'username' :username})
print (checkUsername)
最佳答案
当您执行时,您只需告诉游标在数据库上传达该语句即可。它返回 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
关于python - 如何检查表中尚不存在用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597098/