2

Right now I'm working on a program that allows people to make tests, save them to databases, and then print them. I keep getting the error:

Traceback (most recent call last):
File "C:/Users/Shepard/Desktop/Gradebook.py", line 50, in <module>
qs = QuestionStorage("questions.db")
TypeError: object.__new__() takes no parameters

Anyone have any idea? I'm assuming its somewhere in the QuestionStorage class, but I'm not quite able to figure anything out. This is my first time working with SQLite3, and I'm having quite a bit of trouble, if anyone can help me, that would be great. :)

import sqlite3
class QuestionStorage(object):
    def _init_(self, path):
        self.connection = sqlite3.connect(path)
        self.cursor = self.connection.cursor()

    def Close(self):
        self.cursor.close()
        self.connection.close()

    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.exeute(query)
        self.connection.commit()
        #self.cursor.close()

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

    def GetQuestion(self, index = None):
        self.cursor.execute("""SELECT * FROM questions WEHRE id=?""", (index,))
        res = self.cursor.fetchone()
        return res




print ("TestMaker v.1")
print ("To create a multiple choice test, follow the directions.")
testName = input ("Give your test a name.")
testQ = int(input ("How many questions will be on this test? (Numeric value only.)"))

counter = 1
while counter <= testQ:
    Answer = []
    Answer = [1,2,3,4,5,6]
    Question = input ("What is your question?")
    Answer[1] = input ("What is the first answer?")
    Answer[2] = input ("What is the second answer?")
    Answer[3] = input ("What is the third answer?")
    Answer[4] = input ("What is your last answer?")
    correctAnswer = int(input("Which answer is the correct answer? (1, 2, 3, or 4?)"))
    Answer[5] = Answer[correctAnswer]
    print (Answer[1:6])
    counter +=1
    if __name__ == "__main__":
        qs = QuestionStorage("questions.db")
        qs.CreateDb()    
        qs.AddQuestion(Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])

qs.Close()
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Truwinna
  • 89
  • 1
  • 3
  • 8
  • This almost certainly has nothing to do with sqlite3. If you take out all of the sqlite3 calls, does it still happen? – abarnert Dec 12 '12 at 03:01
  • Also, even after you fix this problem, your code is still not going to work. For example, you create the database `testQ` times, but only `if __name__ == "__main__"`, and then you close it once at the end unconditionally. Also, why are you setting `Answer = []` just to replace it in the next line, only to replace it yet again in the next 6 lines? – abarnert Dec 12 '12 at 03:08
  • I do that because I want it to clear the list every time it loops, so in the case the person wanted a true/false question, they could leave two blank. – Truwinna Dec 12 '12 at 03:30
  • That doesn't make any sense. If they leave the third answer blank, you're going to set `Answer[3]` to `''`; if they don't leave it blank, you're going to set it to whatever they type. Either way, what was the point of having it set to `'3'` beforehand, and what was the point of creating a whole different empty list, assigning it briefly to `Answer`, then throwing it away to replace it with a different list? – abarnert Dec 12 '12 at 17:42

3 Answers3

10

The __init__ method signature is __init__, not _init_ in Python

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
10

It's worth learning how to debug this for yourself.

Nothing in your error message indicates that the problem has anything to do with sqlite3. So, what happens if you take out all the sqlite3 calls in QuestionStorage and run the program?

class QuestionStorage(object):
    def _init_(self, path):
        self.connection = None
        self.cursor = None

    def Close(self):
        pass

    def CreateDb(self):
        query = """CREATE TABLE questions
                 (id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)"""

    def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4):
        pass

    def GetQuestion(self, index = None):
        return [""]

You get the exact same error. That proves that sqlite3 isn't to blame, and hopefully gives you a clue as to how to debug it.

You can see that the problem happens in creating the QuestionStorage from the traceback, and you only do that once in your program… so what if you take out everything else from the rest of the code?

class QuestionStorage(object):
    def _init_(self, path):
        pass

qs = QuestionStorage("questions.db")

Same error. And now it's much more obvious what the problem is. And, even if you can't figure it out yourself, you can post that 5-line stripped-down version to SO.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

I believe you need two underscores in front of init and two after init instead of just one.

kswans
  • 11
  • 1