diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-05 15:27:01 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-04-05 15:27:01 +0300 |
commit | e78315f9d722bc045c17e32b669c3efa84e78271 (patch) | |
tree | 45b830ae262a76e578909b6eff8158df6a373b98 | |
parent | 6c176a26939204380bfe14fbcb81ae395227d2c4 (diff) |
feat: add script to load quiz data from JSON to database
-rw-r--r-- | scripts/load_quiz_data.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/scripts/load_quiz_data.py b/scripts/load_quiz_data.py new file mode 100644 index 0000000..6b0a6c9 --- /dev/null +++ b/scripts/load_quiz_data.py @@ -0,0 +1,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) |