2 回答

TA貢獻1784條經(jīng)驗 獲得超8個贊
fetchall方法返回一個元組列表,無論是一列還是多列。因此,這里的比較if row == username:
永遠不會成立。如果你想要元組的第一個元素,它是通常的,即row[0]
.

TA貢獻1829條經(jīng)驗 獲得超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)
但是,您不需要從數(shù)據(jù)庫中獲取所有記錄。您可以使用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)")
這樣表中就沒有重復(fù)的用戶名了。
添加回答
舉報