From 11fcd12ee85347bdd1a08110a6198d90afa19fb7 Mon Sep 17 00:00:00 2001 From: vaibhav-rm Date: Tue, 25 Feb 2025 22:57:33 +0530 Subject: [PATCH 1/3] sqlite database added --- main.py | 56 ++++++++++++++++++++++++++++++++++++---------- questions.py | 51 +++++++++++++++++++++++++++++++++++++++++ quiz_questions.db | Bin 0 -> 36864 bytes 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 quiz_questions.db diff --git a/main.py b/main.py index fb73097..47235b5 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,35 @@ from questions import questions_list from collections import Counter +import sqlite3 + +# Connect to SQLite database (or create it if it doesn't exist) +conn = sqlite3.connect('quiz_questions.db') +cursor = conn.cursor() + +# Create a table for questions +cursor.execute(''' +CREATE TABLE IF NOT EXISTS questions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + question TEXT NOT NULL, + options TEXT NOT NULL, + correct_answer TEXT NOT NULL, + subject TEXT NOT NULL +) +''') + +class Question: + def __init__(self, question, options, correct_answer, subject): + self.question = question + self.options = options + self.correct_answer = correct_answer + self.subject = subject + + +# Commit changes and close the connection +conn.commit() +conn.close() + class HomeScreen(Screen): pass @@ -45,6 +74,8 @@ def update_subject_counts(self): def open_github_link(self): webbrowser.open('https://github.com/vaibhav-rm/Dcet-prep-app') +import sqlite3 # Add this import at the top of the file + class QuizScreen(Screen): question_text = StringProperty() options = ListProperty() @@ -55,25 +86,26 @@ class QuizScreen(Screen): def __init__(self, **kwargs): super(QuizScreen, self).__init__(**kwargs) - self.questions = questions_list + self.questions = self.load_questions_from_db() # Load questions from the database self.start_new_round() + def load_questions_from_db(self): + conn = sqlite3.connect('quiz_questions.db') + cursor = conn.cursor() + cursor.execute('SELECT question, options, correct_answer, subject FROM questions') + rows = cursor.fetchall() + conn.close() + + # Convert rows to Question objects + return [Question(question=row[0], options=row[1].split(','), correct_answer=row[2], subject=row[3]) for row in rows] + def start_new_round(self): self.current_questions = random.sample(self.questions, 5) self.current_question_index = 0 self.round_score = 0 - self.load_question() + self.load_questions_from_db - def load_question(self): - if self.current_question_index < len(self.current_questions): - question = self.current_questions[self.current_question_index] - self.question_text = question.question - self.options = question.options - self.time_left = 15 - self.start_timer() - else: - self.manager.current = 'result' - self.manager.get_screen('result').update_score(self.round_score) + # The rest of the QuizScreen class remains unchanged def start_timer(self): Clock.schedule_interval(self.update_timer, 1) diff --git a/questions.py b/questions.py index e2680af..e340132 100644 --- a/questions.py +++ b/questions.py @@ -5,6 +5,33 @@ def __init__(self, question, options, correct_answer, subject): self.correct_answer = correct_answer self.subject = subject +import sqlite3 + + +# Connect to SQLite database + +conn = sqlite3.connect('quiz_questions.db') + +cursor = conn.cursor() + +cursor.execute(''' + +CREATE TABLE IF NOT EXISTS questions ( + + id INTEGER PRIMARY KEY AUTOINCREMENT, + + question TEXT NOT NULL, + + options TEXT NOT NULL, + + correct_answer TEXT NOT NULL, + + subject TEXT NOT NULL + +) + +''') + questions_list = [ Question( "The project ______ relieves a project team from most regular work such as planning, tracking, and reporting responsibility?", @@ -271,3 +298,27 @@ def __init__(self, question, options, correct_answer, subject): "Project Management Skills" ) ] + +# Prepare data for insertion + +data_to_insert = [ + + (q.question, ','.join(q.options), q.correct_answer, q.subject) for q in questions_list + +] + + +# Insert questions into the database + +cursor.executemany(''' + +INSERT INTO questions (question, options, correct_answer, subject) VALUES (?, ?, ?, ?) + +''', data_to_insert) + + +# Commit changes and close the connection + +conn.commit() + +conn.close() \ No newline at end of file diff --git a/quiz_questions.db b/quiz_questions.db new file mode 100644 index 0000000000000000000000000000000000000000..388b7d5184937b12f84f84655d4f9c040db196e5 GIT binary patch literal 36864 zcmeHP+ix4$c^~RR>ds_0Ue|S9p2YRaUXNtE%64n7ZHAI4t=76(q~dk!#5)`wlCzo% zJr_~jWOp;(wAeNY(xNYYXcH9ZKTsq_8O3;w|{FKz4|8pM#_E?Z^O)>3(*Hp{4x$53kOsB=|1G_k0IawAI|1fRk*_%R*NYn zQ&iWondhKIVNTm|xxri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bqoCQIO2lf6o4H(qcaPYyB%bu(3=6Y^ws^R= zPi3UL{`m2S=r;%pOil}ddVU>Ri@P}2k=wwcWI~2hW*X@ON@I!u1Nja`qEL;~))*wF6oN*dp;o0V!p-^BedUuw}F; z5mpEk>^$xNEW&fOf#9;r*^)_NHc`1`mFa){Kx+9Uh09xKavdI`g+t_EGi=HoQaTEc zJ5=O~qbF05dMB~uIc!s0UVTXh>KL=hJ zo{650gxeQbNQAmb;=th*IUF+8!9#$MLy+J^qJwfQf~7jd1*||>P3(o;CXEp!Iia+< zt&0ZDtiiL3HYC4PC?G*WZzXzL#T%W)blyLC9J>76{TDy?P_W?pBKjtwCLfGU;DUx6 z+EksJ;gbeNk2-_r#^jE{$4CAMPx|P25L7265J4Q0IgvQxStfebNYMse=utCVRk1RN z5&{YZzfBNy6Me|W;b8(YB_dc*Xz<{&<25N8fOg2TK84&7EFsA@q>GZs7*ovlGxUqm z37-GIFJ(_oY>ofs*#C^ZJNo-0?~e=&uMB-P^S#XN^mo&xL3QBA13UfS=^sx0b81d{ zzq^6|-r0Sr*nL5m?-4XMW}L zPlE(}uE!+c&e4~EcYYmv(%tH*UjyquJ=IgU!Ba=1ddfFmd&&w)ZuL$f$!8%|Jyl4u z`N#`N%3UoaDRr}uWCL3M5f+l%e9{U@)*nG3$y)CglB{C;yHZH90xi^Yg(S<}C?vV@ z*b7Ns{b&nGmbzU?@(P&zNhu^*q{!ju6p}0)(Lxf6|Bp+*A!T%Y?Xdg^ymgXsRzzTWn_EjjKJBstE}k{tVp zN{%nx>we`)6TH$3C0G``prizAPpQ_f$`cdXrEjGnGM(Sde1Do?h$PA4IjUU^;G@xiH{%0>Xn~9@#$x@da7i_Pd)OQ6<_LV(aJmB zELwRRnVd&hwDQ)IR Date: Tue, 25 Feb 2025 23:04:21 +0530 Subject: [PATCH 2/3] removed dependency on questions file --- main.py | 39 ++++++++++++++++++++++++++++++--------- quiz_questions.db | Bin 36864 -> 45056 bytes 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 47235b5..8caaf75 100644 --- a/main.py +++ b/main.py @@ -8,7 +8,7 @@ from kivy.animation import Animation from kivy.clock import Clock import random -from questions import questions_list +import sqlite3 # Add this import at the top of the file from collections import Counter import sqlite3 @@ -54,9 +54,22 @@ def on_pre_enter(self): self.update_subject_counts() def update_subject_counts(self): - subjects = [q.subject for q in questions_list] - self.subject_counts = dict(Counter(subjects)) - self.total_questions = len(questions_list) + conn = sqlite3.connect('quiz_questions.db') + cursor = conn.cursor() + + # Get total question count + cursor.execute('SELECT COUNT(*) FROM questions') + self.total_questions = cursor.fetchone()[0] + + # Get subject counts + cursor.execute('SELECT subject, COUNT(*) FROM questions GROUP BY subject') + rows = cursor.fetchall() + conn.close() + + # Update subject_counts dictionary + self.subject_counts = {row[0]: row[1] for row in rows} + + # Clear existing widgets and add new ones self.ids.subject_counts_box.clear_widgets() for subject, count in self.subject_counts.items(): self.ids.subject_counts_box.add_widget( @@ -74,7 +87,6 @@ def update_subject_counts(self): def open_github_link(self): webbrowser.open('https://github.com/vaibhav-rm/Dcet-prep-app') -import sqlite3 # Add this import at the top of the file class QuizScreen(Screen): question_text = StringProperty() @@ -103,9 +115,18 @@ def start_new_round(self): self.current_questions = random.sample(self.questions, 5) self.current_question_index = 0 self.round_score = 0 - self.load_questions_from_db - - # The rest of the QuizScreen class remains unchanged + self.load_question() # Call load_question to display the first question + + def load_question(self): + if self.current_question_index < len(self.current_questions): + question = self.current_questions[self.current_question_index] + self.question_text = question.question + self.options = question.options + self.time_left = 15 + self.start_timer() + else: + self.manager.current = 'result' + self.manager.get_screen('result').update_score(self.round_score) def start_timer(self): Clock.schedule_interval(self.update_timer, 1) @@ -138,7 +159,7 @@ def next_question(self): self.current_question_index += 1 self.ids.feedback.text = "" self.ids.next_button.disabled = True - self.load_question() + self.load_question() # Call load_question to load the next question class ResultScreen(Screen): round_score = NumericProperty(0) diff --git a/quiz_questions.db b/quiz_questions.db index 388b7d5184937b12f84f84655d4f9c040db196e5..6eeb43cf6039fd316c5130b0b95ffa1fc92a44e2 100644 GIT binary patch delta 512 zcmWlUT}V>_9L4Yb@7~?*-re0yE@W@^*o$T5xiVUR|)I5JZdTG*}TStwH(-enXraSN9PH3oIPfRhX;?pA{dOZ z;&iv4{%O@kMgapEDo~sddape3gA;j6+d9j_%J4515 ju3Z=>v~eOnM%clRdQK6xPxnp{wsEfaFrk%07mNP@_h_p; delta 75 zcmZp8z|^pSX@WE>3j+fK=R^g2MwX2UOZbIY`JV#?7&!TV^MB+2$p41_`DQ_dEBu>Z f$%_gwwrnzB31H@De>a)EL4IT53HHtJ+J!g(JuMgK From 0d7e1ffacdc40842832f7bf7e172cfed39275078 Mon Sep 17 00:00:00 2001 From: vaibhav-rm Date: Tue, 25 Feb 2025 23:12:43 +0530 Subject: [PATCH 3/3] closed connectors at end --- main.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 8caaf75..45d152e 100644 --- a/main.py +++ b/main.py @@ -8,12 +8,9 @@ from kivy.animation import Animation from kivy.clock import Clock import random -import sqlite3 # Add this import at the top of the file +import sqlite3 from collections import Counter -import sqlite3 - -# Connect to SQLite database (or create it if it doesn't exist) conn = sqlite3.connect('quiz_questions.db') cursor = conn.cursor() @@ -36,9 +33,6 @@ def __init__(self, question, options, correct_answer, subject): self.subject = subject -# Commit changes and close the connection -conn.commit() -conn.close() class HomeScreen(Screen): pass @@ -189,3 +183,5 @@ def build(self): if __name__ == '__main__': dectquiz().run() +conn.commit() +conn.close()