gpt4 book ai didi

python - 如何检查sqlite数据库中的用户名? Python

转载 作者:太空宇宙 更新时间:2023-11-03 14:35:11 25 4
gpt4 key购买 nike

我正在尝试检查数据库中的用户名,当我这样做时,它适用于第一个用户名,但在它不起作用之后,我想我明白为什么,但我无法找到替代方案。这是我的代码:

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:

关于python - 如何检查sqlite数据库中的用户名? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47005179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com