出现无法下标的错误,例如“ cursor.execute”,cursor.fetchall()不带任何参数(2个政府)

时间:2019-02-28 13:10:39

标签: python sql sqlite

我正在为一个项目制作python sql程序,并且遇到登录错误问题。这是整个登录部分

print("Welcome to the program")
    while True:
        print("If this is your first time playing, you may create an account. If not, log in to an existing one. login/create. If you'd like to exit, type in exit.")
        answer = input()
        if answer == 'login':
            while True:
                db = sqlite3.connect('data/users')
                cursor.execute('''
                        CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, username TEXT,
                                        password TEXT, score TEXT)
                        ''')
                username = input("Insert username")
                password = input("Insert password")
                id = cursor.lastrowid
                user_search = cursor.fetchall('''SELECT * FROM users WHERE id = 1 AND username = ? AND password = ?''', (username, password))
                cursor.execute(user_search[(username),(password)])
                data=cursor.fetchall()
                if data is None:
                    print("Nothing found with an id of 1, nor are there matchign results for the username and password.")
                else:
                    print("None found.")
                    #https://stackoverflow.com/questions/2440147/how-to-check-the-existence-of-a-row-in-sqlite-with-python Code reference, this site helped me find out what I was doing wrong
        elif answer == 'create':
            print("You may create an account if there are none.")
            username = input("Create a new username. Case sensitive.")
            password = input("Create a new password. Case sensitive.")
            user_search = cursor.execute('''SELECT * FROM users WHERE id = 1 AND username = ? AND password = ?''', (username, password))
            result = cursor.fetchall()
            cursor.execute(user_search[(username),(password)])
            if result:
                for i in result:
                    print("There is only one user slot allowed. Said slot is taken. You may not create an account.")
                    break
            else:
                print("User slot free. Appending credentials. You have created an account.")
                credentials =[(username),(password)]
                cursor.executemany('''INSERT INTO users(username, password) VALUES(?,?)''', (users))

        else:
            print("Input not recognised.")

特别是本节:

 username = input("Insert username")
            password = input("Insert password")
            id = cursor.lastrowid
            user_search = cursor.fetchall('''SELECT * FROM users WHERE id = 1 AND username = ? AND password = ?''', (username, password))
            cursor.execute(user_search[(username),(password)])
            data=cursor.fetchall()
            if data is None:
                print("Nothing found with an id of 1, nor are there matching results for the username and password.")
            else:
                print("None found.")

我不习惯使用python进行编码,所以我不知道为什么会出现这些错误。 SQL对我来说也很新。 我收到“ fetchall接受0个参数,给出2个参数”,却找不到可行的解决方案

1 个答案:

答案 0 :(得分:0)

fetchall()不带参数,用于获取查询结果。首先execute查询,然后提取。

username = input("Insert username")
password = input("Insert password")
id = cursor.lastrowid    
cursor.execute('''SELECT * FROM users WHERE id = %s AND username = %s
                                AND password = %s''' % (id, username, password))
data = cursor.fetchall()