简体   繁体   English

查询需要7个位置参数,但给出了8个

[英]query takes 7 positional arguments but eight were given

I keep getting the error: 我不断收到错误:

Traceback (most recent call last):
  File "C:\Users\Shepard\Desktop\Final Program\TestMaker.py", line 53, in <module>
    qs.AddQuestion(None, Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])
TypeError: AddQuestion() takes 7 positional arguments but 8 were given

Seems easy enough to figure out. 似乎很容易找出来。 I just went through all of my query statements and counted seven positional arguments. 我只是遍历了所有查询语句并计算了七个位置参数。 Every time. 每次。 Including the id. 包括ID。 What is this black magic? 这是什么黑魔法? I'm so lost, it's not even comical. 我很失落,甚至都不可笑。

Here's all the code that generates a sqlite3 query. 这是生成sqlite3查询的所有代码。

def CreateDb(self):
    query = """CREATE TABLE Questions
             (id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)"""
    self.cursor.execute(query)
    self.connection.commit()

def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer):
    self.cursor.execute("""INSERT INTO questions
                            VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", [None, Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer])
    self.connection.commit()

if __name__ == "__main__":
    qs = QuestionStorage(testName + ".db")
    qs.CreateDb()    
    qs.AddQuestion(None, Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])

I'm so lost right now :S 我现在很迷失:S

AddQuestion is a bound method of an object (the object being qs in your case), not an unbound function. AddQuestion是对象(在您的情况下为qs )的绑定方法,而不是未绑定的函数。 That means when you call it, qs is automatically passed as the first positional argument, before the ones that you pass explicitly. 这意味着当您调用它时, qs将自动作为第一个位置参数传递,而在您显式传递的参数之前。 That's why methods are usually written with self as the first argument. 这就是为什么方法通常以self作为第一个参数编写的原因。

You need to change 你需要改变

qs.AddQuestion(None, Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])

to

qs.AddQuestion(Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])

This doesn't require None (AFAICT): 这并不需要None (AFAICT):

qs.AddQuestion(None, Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])

Also, you'll have problems later in your query in .AddQuestion - I count 8 in the following for the place holders... 另外,稍后您在.AddQuestion中的查询中也会遇到问题-我在下面将占位符数为8 ...

VALUES (?, ?, ?, ?, ?, ?, ?, ?)

and 7 for the parameters... So, not sure what you're trying to do! 和7作为参数...因此,不确定您要做什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM