我正在开发一个能够通过将问题导出到数据库来进行测验的程序。我在网上看了一会儿,它说在 python 中导入或导出到数据库的最简单方法之一是使用 SQLite3 插件,所以我正在尝试。这是我第一次我在 python 中使用了 SQLite3 插件,并且我在 self.connection.commit() 上不断收到语法错误:
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()
如果我通过在它前面添加 # 来将它变成注释,它会告诉我这里的打印是一个语法错误:
print ("Would you like to make a test? Or would you like to take a test?")
也许是我的缩进,还是我做错了什么?
import squlite3
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,))
print ("Would you like to make a test? Or would you like to take a test?")
testTaker = input ("To create a test, type Create. To take a test, type Take.")
if testTaker == "Create":
testName = input ("Give your test a name.")
testQ = int(input ("How many questions will be on this test? (Numeric value only.)"))
testType = input ("Will this test be multiple choice? (y/n)")
if testType == "N" or "n":
counter = 1
qs = QuestionStorage("questions.db")
qs.CreateDb()
counter = 1
while counter >= testQ:
Answer = []
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 = input("Which answer is the correct answer? (1, 2, 3, or 4?)")
Answer[5] = Answer[correctAnswer]
qs.AddQuestion(Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])
counter +=1
else:
然后在else之后,我将获得读取数据库的代码以进行测试。
如果有人可以帮助我解决这个问题,那就太好了。现在我只是想让它达到我可以在调试中运行它的地步。