Apr-13-2019, 10:54 PM
the database has three tables, Question, Choices and Answers
If you run sqlite3 from command line, you can see the different tables:
for example line 39:
uses textvariable self.current_question which is set on line 69:
the others are similar
on line 62:
textvariable is self.get_next_question
If you run sqlite3 from command line, you can see the different tables:
SQLite version 3.11.0 2016-02-15 17:29:24 Enter ".help" for usage hints. sqlite> .schema CREATE TABLE Questions (QuestionId, QuizCode, Unused, Question); CREATE TABLE Choices (QuestionId, QuizCode, SeqLetter, Choice); CREATE TABLE Answers (QuestionId, QuizCode, Answer, Unused);As you can see, each table has QuestionId in common, yo you can link everything together using that:
sqlite> select * from Questions where QuestionId = '1'; 1|Q||Which Of these describes an element best? sqlite> sqlite> select * from choices where QuestionId = '1'; 1|C|A|Building Block 1|C|B|Made Of Only One Type Of Atom 1|C|C|Contains No Chemical Bonds 1|C|D|Pure Substance sqlite> sqlite> sqlite> select * from answers where QuestionId = '1'; 1|A|B| sqlite>The second column contains the quiz code (same as spreadsheet, Q, C or A for question, choice, or answer)
def get_question(self, question_number): # Set up SQL statements (based on question_number) qsql = f''' SELECT Question FROM Questions WHERE QuestionId = '{question_number}' ''' csql = f''' SELECT SeqLetter, Choice FROM Choices WHERE QuestionId = '{question_number}' ORDER BY SeqLetter ''' asql = f''' SELECT Answer FROM Answers WHERE QuestionId = '{question_number}' ''' try: # create a cursor cursor = self.dbcon.cursor() # fetch question using SQL statement qsql cursor.execute(qsql) # fetch result of query into variable question question = cursor.fetchone()[0] # Create a list to hold all choices choices = [] # fetch choices using SQL statement csql cursor.execute(csql) # move results into choices list while True: choice = cursor.fetchmany() if not len(choice): break choices.append(list(choice[0])) # fetch answer using SQL statement asql cursor.execute(asql) # fetch result of query into variable answer answer = cursor.fetchone()[0] return question, choices, answer except TypeError: print(f'Invalid question_number {question_number}')Variables are set from the query
for example line 39:
qlabel = tk.Label(frame1, textvariable=self.current_question)
uses textvariable self.current_question which is set on line 69:
self.current_question.set(f'Question {question_number}: {question}')
the others are similar
on line 62:
response = tk.Label(frame1, textvariable=self.get_next_question)
textvariable is self.get_next_question