summaryrefslogtreecommitdiff
path: root/scripts/load_quiz_data.py
blob: 6b0a6c96d9517ff0510705d60a94bbbc73b518eb (plain)
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
import json
import sqlite3
from pathlib import Path

def load_questions(json_path, db_path):
    # Connect to SQLite database
    conn = sqlite3.connect(db_path)
    c = conn.cursor()
    
    # Create questions table if not exists
    c.execute('''CREATE TABLE IF NOT EXISTS questions
                 (id INTEGER PRIMARY KEY,
                  text TEXT,
                  option1 TEXT,
                  option2 TEXT,
                  option3 TEXT,
                  option4 TEXT,
                  correct_index INTEGER,
                  requirement TEXT,
                  explanation TEXT)''')
    
    # Load JSON data
    with open(json_path) as f:
        questions = json.load(f)
    
    # Insert questions
    for q in questions:
        c.execute('''INSERT INTO questions 
                     (id, text, option1, option2, option3, option4, correct_index, requirement, explanation)
                     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''',
                  (q['id'], q['text'], q['option1'], q['option2'], q['option3'], q['option4'],
                   q['correct_index'], q.get('requirement', ''), q.get('explanation', '')))
    
    conn.commit()
    conn.close()
    print(f"Loaded {len(questions)} questions into database")

if __name__ == "__main__":
    project_root = Path(__file__).parent.parent
    json_path = project_root / "quiz.json"
    db_path = project_root / "demoon.db"
    
    load_questions(json_path, db_path)