我希望用户写下他们的用户名并查看他们的输入是否在我的表格中,但我不知道如何搜索他们的输入。
如何做到这一点(这是选择1的位置)?
import sqlite3
import sys
conn= sqlite3.connect('log_accounts.db')
c= conn.cursor()
def login():
c.execute('CREATE TABLE IF NOT EXISTS account(forename, age, username , year_group, password)')
c.execute("INSERT INTO account VALUES ('Hannah', '16', 'Han16', '11', '1234')")
conn.commit()
print("----------------------")
print("| 1- Log in |")
print("| 2- Register |")
print("| 3- Exit |")
choice= input("Please select a choice :\n")
if choice =='1':
username= input("Please enter your username: \n")
password= input("Please enter your password: \n")
results=c.execute("SELECT username, password"
"FROM accounts WHERE username= ?", (username,))
print(results)
c.close()
conn.close()
login()
答案 0 :(得分:1)
execute()
返回一个游标对象,您必须迭代该对象以获取行。每行都是一个包含列值的元组(在本例中为两个):
results = c.execute("SELECT username, password FROM ...")
for row in results:
print("found: name = {}, password = {}".format(row[0], row[1])
break
else:
print("not found")
答案 1 :(得分:0)
```
c.execute('CREATE TABLE IF NOT EXISTS account(forename, age, username ,
year_group, password)')
c.execute("INSERT INTO account VALUES ('Hannah', '16', 'Han16', '11',
'1234')")
conn.commit()
print("----------------------")
print("| 1- Log in |")
print("| 2- Register |")
print("| 3- Exit |")
username= input("Please select a UserName:\n")
password= input("Please select a Password:\n")
cursor.execute(f"SELECT password FROM account WHERE username={username}")
results=cursor.fetchone()[0]
if results:
if results==password:
print("login success!")
else:
print("password is wrong")
else:
print("username is wrong")
```