2 回答
TA贡献1784条经验 获得超8个赞
fetchall方法返回一个元组列表,无论是一列还是多列。因此,这里的比较if row == username:
永远不会成立。如果你想要元组的第一个元素,它是通常的,即row[0]
.
TA贡献1829条经验 获得超6个赞
返回的记录是元组,因此您需要改用row[0] == username:
def dataRead():
username = user.get()
password = pword.get()
c.execute("SELECT username FROM userInfo")
data = c.fetchall()
found = False
for row in data:
if row[0] == username:
found = True
Label(regWin, text = "Sorry, username already in use...\nTry another one.", fg = "red").pack()
print(row)
break
if not found:
dataEntry(username, password)
但是,您不需要从数据库中获取所有记录。您可以使用WHERE子句来获取所需的记录:
def dataRead():
username = user.get()
password = pword.get()
c.execute('SELECT username FROM userInfo WHERE username = ?', (username,))
data = c.fetchone()
if data:
Label(regWin, text = "Sorry, username already in use...\nTry another one.", fg = "red").pack()
print(data)
else:
dataEntry(username, password)
最好使username字段成为一个独特的字段:
def createTable():
c.execute("CREATE TABLE IF NOT EXISTS userInfo(username TEXT PRIMARY KEY, password TEXT)")
这样表中就没有重复的用户名了。
添加回答
举报