2017-10-20 9 views
0

私はPython 3.6のために死んだ単純なログインと登録スクリプトを書こうとしています。しかし、私はこのバグを取り除くことができません...私は登録するたびに、エラーはありませんが、私は再びそれを実行し、同じユーザー名を使用すると、それは既に存在するメッセージを表示しません... 私はあなたが実際に任意のコードを実行せずにfetchall()を実行している...ここにコードがありますあまりにもちょうどので、私は(c.fetchall()を使用して)そこにあるものを見ることができ、出力全体のDBにSQLite 3/Python3.6 - 登録とログインスクリプト(sqlとpython初心者)

import sqlite3 as sql, os 

def dirCheck(): 

    exDir=False 

    for count in os.listdir(): 
     if count == 'sql': 
      exDir=True 
    if exDir == False: 
     os.mkdir('sql') 



dirCheck() 


cnct=sql.connect('./sql/usrAcc.db') 
c=cnct.cursor() 


def newTable(): 
    c.execute('CREATE TABLE IF NOT EXISTS users(username TEXT, password TEXT)') 


newTable() 

f=False 
t=True 
valid=f 
taken=f 





def credsWrite(username, password): 
    c.execute('INSERT INTO users(username, password) VALUES (?, ?)', 
       (username, password)) 
    cnct.commit() 
    for row in c.fetchall(): 
     print(row) 
    c.close 
    cnct.close 



def userReg(): 
    global valid, taken 

    print(fancy[1]) 
    while not valid: 
     print(fancy[3]) 
     username=str(input('>> ')) 
     for row in c.fetchall(): 
      if str(username) == str(row[0]): 
       print('Sorry, but that username has already been taken...\nPlease enter another\n> ') 
       taken=True 
     if not taken: 
      print(fancy[4]) 
      password=str(input('>> ')) 
      valid=True 
     credsWrite(username,password) 

fancy=[""" 
================ 
| SQL LOGIN TEST | 
================ 
""",""" 
========== 
| REGISTER | 
========== 
""",""" 
======= 
| LOGIN | 
======= 
""",""" 
================ 
| ENTER USERNAME | 
================ 
""",""" 
================ 
| ENTER PASSWORD | 
================ 
"""] 


def startUp(): 
    print(fancy[0]) 
    chosen=False 
    while not chosen: 
     opt=int(input('\n Please choose one of the options below:\n\n-> Register for a new account [1]\n-> Login to an existing account [2]\n\nPlease type a number...\n\n>> ')) 
     if opt==1: 
      userReg() 
      chosen=True 
     elif opt==2: 
      login() 
      chosen=True 
     else: 
      print('\n\nPLEASE TYPE EITHER 1 OR 2...\n ') 


if __name__ == "__main__": 
    startUp() 

答えて

0

をそれを得るカント。あなたが唯一まだ何のために見ていないだけでカーソルオブジェクトを介して、見ている

for row in c.fetchall(): 

を持って

これにfetchallを使用するには、SELECTステートメントを追加する必要があります。例えば、私は1つの行を追加し、1行を変更して、コードを変更し、それが走った(発生する別の構文エラーがありますが):

all_users = c.execute('SELECT * FROM users') 
for row in all_users.fetchall(): 

(他の構文エラーがあるあなたは関係なく、credsをを書き込もうとしていることユーザ名が有効であったかどうかの)


注:あなたが考慮すべき何か他のもの:

の代わりにあなただけのユーザー名は、すでにあなたのSQLに存在するかどうかを検索できfetchallの結果をループ。これにより、次の部分もちょうどelseになります。

username=str(input('>> ')) 
taken = c.execute('SELECT username FROM users WHERE username=?', (username,)) 
if taken: 
    print('Sorry, but that username has already been taken...\nPlease enter another\n> ') 

(username,)は(python docs for sqlite3に初期記載)パラメータです。ユーザからの値を使用してSQL injectionを避けるときは、常にパラメータを使用する必要があります。それはタプルにするためにカンマで括弧内にあります。