[英]How to check username in sqlite database? Python
我正在嘗試檢查數據庫中的用戶名,當我這樣做時,它適用於第一個用戶名,但在它不起作用之后,我想我明白為什么,但我無法找到替代方案。 這是我的代碼:
import sqlite3
conn = sqlite3.connect('tutorial.db')
c = conn.cursor()
def username():
global limit
global usernameinput
usernameinput = input("What would you like your username to be?")
limit = 0
select_all_tasks(conn)
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS stuffToPlot(username TEXT,
password TEXT)')
conn.close
def select_all_tasks(conn):
global limit
global passwordinput
global rows
c.execute("SELECT * FROM stuffToPlot")
rows = c.fetchall()
for row in rows:
print(row)
if usernameinput in row:
print("Username Taken")
username()
else:
if limit < 1:
passwordinput = input("What would you like your password to
be?")
limit = limit + 1
def data_entry():
global passwordinput
global row
c.execute("INSERT INTO stuffToPlot VALUES(?, ?);",(usernameinput,
passwordinput))
conn.commit()
c.close()
conn.close()
create_table()
username()
select_all_tasks(conn)
data_entry()
沒有錯誤,只是沒有注冊用戶名已經在數據庫中。
您需要縮進 if 語句和以下所有行以使其工作,否則只測試最后一行。
for row in rows:
print(row)
if usernameinput in row:
print("Username Taken")
你也可以讓它更簡單:
# select only the field "username" from the table
c.execute("SELECT username FROM stuffToPlot")
# build a set with all names, from the results given as [('name1',), ('name2',), ('name3',)]
names = {name[0] for name in c.fetchall()}
if usernameinput in names:
print("Username Taken")
username()
else:
if limit < 1:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.