2017-12-28 27 views
1

사용자가 사용자 이름을 쓰고 입력 내용이 표에 있는지 확인하고 입력을 검색하는 방법을 모르겠습니다.사용자 입력 검색

어떻게해야합니까 (선택 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() 
+1

당신은 당신의 문제가 무엇인지 말을하지 않습니다하지만, SELECT 쿼리에서'password' 뒤에 공백이 필요하다고 생각합니다. –

+0

오타가 의심 스러울 수도 있습니다. '계정으로부터 사용자 이름을 선택하십시오.'계정 이름이 테이블입니다. – Mangohero1

답변

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") 
+0

완벽하게 작동하며 빠른 답장을 보내 주셔서 감사합니다. – Pintsize21

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") 

```