Python Forum
[Tkinter] TKINTER quiz using sqlite3 database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] TKINTER quiz using sqlite3 database
#37
the database has three tables, Question, Choices and Answers
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
Reply


Messages In This Thread
TKINTER quiz using sqlite3 database - by hezza_23 - Apr-01-2019, 01:02 PM
RE: TKINTER quiz using sqlite3 database - by Larz60+ - Apr-13-2019, 10:54 PM
RE: TKINTER quiz using sqlite3 database - by Hilal - Nov-29-2021, 09:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 5,087 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,844 Jan-11-2022, 08:37 PM
Last Post: deanhystad
Question [Tkinter] data enterred through gui is not storing in sqlite3 database Hilal 21 8,005 Dec-15-2021, 08:48 PM
Last Post: Hilal
  Help with PySimpleGUI INSERT INTO sqlite3 database jrbond 5 7,313 Jul-20-2020, 01:24 PM
Last Post: jrbond
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 6,813 Jan-23-2019, 08:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020