1

我不断收到错误:

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

似乎很容易弄清楚。我刚刚浏览了所有查询语句并计算了七个位置参数。每次。包括身份证。这是什么黑魔法?我很迷茫,这甚至不是可笑的。

这是生成 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

4

2 回答 2

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])
于 2012-12-12T18:22:54.440 回答
2

这不需要None(AFAICT):

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

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

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

和 7 的参数......所以,不知道你要做什么!

于 2012-12-12T18:27:47.617 回答