-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
60 lines (49 loc) · 1.7 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import sqlite3
import random
db_name = "database.db"
def load_questions(filename):
"""
Load questions from a JSON file and return them as a list of dictionaries.
Each dictionary represents a question with keys: 'level', 'question', 'options', 'answer'.
"""
with open(filename, 'r') as json_file:
questions = json.load(json_file)
return questions
easy_length, hard_length = 0, 0
def init_db():
global easy_length, hard_length
print("loading content to db ...")
connection = sqlite3.connect('database.db')
with open('schema.sql') as f:
connection.executescript(f.read())
cur = connection.cursor()
for q in load_questions("questions.json"):
if q["level"] == "Hard": hard_length += 1
else: easy_length += 1
table_name = "questions_" + q["level"].lower()
cur.execute(f"INSERT INTO {table_name} (json) VALUES ('{json.dumps(q)}')")
connection.commit()
connection.close()
print("db loaded succesfully ...")
easy_index, hard_index = 0, 0
def fetch_question(diff):
global easy_index, hard_index
table_name, index = 0, 0
if diff == "easy":
table_name = "questions_hard"
easy_index = easy_index % easy_length + 1
index = easy_index
elif diff == "hard":
table_name = "questions_hard"
hard_index = hard_index % hard_length + 1
index = hard_index
else:
raise Exception("invalid diff level in fetch_questions")
query = f"SELECT json FROM {table_name} WHERE rowid = {index}"
conn = sqlite3.connect(db_name)
res = conn.execute(query).fetchone()
conn.close()
if res == None:
raise Exception("query failed!")
return json.loads(res[0])