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

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

mycursor= mydb.cursor()
username=input('username')
checkusername= mycursor.execute('SELECT username FROM users WHERE username = %(username)s',{'username' :username})
print (checkusername)
jiangpingshe 回答:如何检查表中不存在用户名?

执行时,您只是告诉游标在DB上传达该语句。它返回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
本文链接:https://www.f2er.com/2818472.html

大家都在问