无法从数据库记录中读取 sqlite3 python
Unable to read from database records sqlite3 python
这是我第一次使用 sqlite3 并且我正在使用数据库。我正在制作一个在线井字游戏,这样我就可以和我的朋友一起玩了。在 def dataRead():
中,我试图读取用户从注册 window 中输入的数据,并且我正在尝试检查数据是否已保存在数据库中。如果是,用户必须输入另一个用户名。
def register():
##initialising sqlite
con = sqlite3.connect("userData.db")
c = con.cursor()
def submit():
#creating tables in the database
def createTable():
c.execute("CREATE TABLE IF NOT EXISTS userInfo(username TEXT, password TEXT)")
def dataRead():
username = user.get()
password = pword.get()
c.execute("SELECT username FROM userInfo")
data = c.fetchall()
try:
for row in data:
if row == username:
Label(regWin, text = "Sorry, username already in use...\nTry another one.", fg = "red").pack()
print(data)
else:
dataEntry(username, password)
except TypeError:
dataEntry(username, password)
def dataEntry(username, password):
c.execute("INSERT INTO userInfo(username, password) VALUES (?, ?)", (username, password))
con.commit()
createTable()
dataRead()
我尝试使用c.fetchall()
读取userInfo
中username
的记录,这样程序可以检查用户名是否可用,但似乎没有工作(至少对我来说)。
fetchall 方法returns 元组列表,无论选择一列还是多列。因此这里的比较 if row == username:
永远不会成立。如果你想要元组的第一个元素,它是通常的,即 row[0]
.
返回的记录是元组,所以需要用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)")
因此 table 中没有重复的用户名。
这是我第一次使用 sqlite3 并且我正在使用数据库。我正在制作一个在线井字游戏,这样我就可以和我的朋友一起玩了。在 def dataRead():
中,我试图读取用户从注册 window 中输入的数据,并且我正在尝试检查数据是否已保存在数据库中。如果是,用户必须输入另一个用户名。
def register():
##initialising sqlite
con = sqlite3.connect("userData.db")
c = con.cursor()
def submit():
#creating tables in the database
def createTable():
c.execute("CREATE TABLE IF NOT EXISTS userInfo(username TEXT, password TEXT)")
def dataRead():
username = user.get()
password = pword.get()
c.execute("SELECT username FROM userInfo")
data = c.fetchall()
try:
for row in data:
if row == username:
Label(regWin, text = "Sorry, username already in use...\nTry another one.", fg = "red").pack()
print(data)
else:
dataEntry(username, password)
except TypeError:
dataEntry(username, password)
def dataEntry(username, password):
c.execute("INSERT INTO userInfo(username, password) VALUES (?, ?)", (username, password))
con.commit()
createTable()
dataRead()
我尝试使用c.fetchall()
读取userInfo
中username
的记录,这样程序可以检查用户名是否可用,但似乎没有工作(至少对我来说)。
fetchall 方法returns 元组列表,无论选择一列还是多列。因此这里的比较 if row == username:
永远不会成立。如果你想要元组的第一个元素,它是通常的,即 row[0]
.
返回的记录是元组,所以需要用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)")
因此 table 中没有重复的用户名。