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

时间:2012-12-12 18:17:38

标签: python sqlite python-3.x

我一直收到错误:

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

似乎很容易搞清楚。我刚刚查看了所有查询语句并计算了七个位置参数。每次。包括id。这个黑魔法是什么?我迷路了,甚至都不可笑。

以下是生成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])

我现在迷路了:S

2 个答案:

答案 0 :(得分:4)

AddQuestion是对象的绑定方法(在您的情况下对象为qs),而不是未绑定的函数。这意味着当你调用它时,qs会自动作为第一个位置参数传递,然后显式传递。这就是为什么方法通常用self作为第一个参数编写的。

您需要更改

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

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

答案 1 :(得分:2)

这不需要None(AFAICT):

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

另外,您稍后会在.AddQuestion的查询中遇到问题 - 我在下面为占位符计数8 ...

VALUES (?, ?, ?, ?, ?, ?, ?, ?)
参数

和7 ......所以,不确定你要做什么!