-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
64 lines (59 loc) · 1.69 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
61
62
63
64
import sqlite3
import os
def create_connection():
conn = None
try:
db_path = os.path.abspath(os.getcwd()) + "\\quiz_database.db"
conn = sqlite3.connect(db_path)
return conn
except sqlite3.Error as e:
print(e)
return conn
def create_quiz(topic, data):
conn = create_connection()
cursor = conn.cursor()
for entry in data:
cursor.execute('''
INSERT INTO quiz_table (topic, question, option_a, option_b, option_c, option_d, correct_answer)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (topic, entry['q'], entry['A'], entry['B'], entry['C'], entry['D'], entry['correct']))
conn.commit()
conn.close()
def get_quiz(topic):
conn = create_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT question, option_a, option_b, option_c, option_d, correct_answer
FROM quiz_table
WHERE topic = ?
''', (topic,))
rows = cursor.fetchall()
conn.close()
quiz = []
for row in rows:
quiz.append({
'question': row[0],
'option_a': row[1],
'option_b': row[2],
'option_c': row[3],
'option_d': row[4],
'correct_answer': row[5]
})
return quiz
def delete_quiz(topic):
conn = create_connection()
cursor = conn.cursor()
cursor.execute('''
DELETE FROM quiz_table
WHERE topic = ?
''', (topic,))
conn.commit()
conn.close()
def get_topics():
conn = create_connection()
cursor = conn.cursor()
cursor.execute('SELECT DISTINCT topic FROM quiz_table')
topics = cursor.fetchall()
conn.commit()
conn.close()
return [topic[0] for topic in topics]