-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign_units_topics_direct.py
More file actions
76 lines (60 loc) · 2.25 KB
/
assign_units_topics_direct.py
File metadata and controls
76 lines (60 loc) · 2.25 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
import random
import sqlite3
from pathlib import Path
def assign_units_topics():
# Path to the database
db_path = Path('instance/researchnest.db')
if not db_path.exists():
print(f"Database not found at {db_path}")
return
try:
# Connect to the database
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
# Get all questions
cursor.execute("SELECT id FROM questions")
questions = cursor.fetchall()
if not questions:
print("No questions found in the database.")
return
# Get all units
cursor.execute("SELECT id FROM units")
units = cursor.fetchall()
if not units:
print("No units found in the database.")
return
# Get all topics with their unit_id
cursor.execute("SELECT id, unit_id FROM topics")
topics = cursor.fetchall()
if not topics:
print("No topics found in the database.")
return
# Group topics by unit_id
topics_by_unit = {}
for topic_id, unit_id in topics:
if unit_id not in topics_by_unit:
topics_by_unit[unit_id] = []
topics_by_unit[unit_id].append(topic_id)
# Update questions with random units and topics
updated_count = 0
for (question_id,) in questions:
# Randomly select a unit with topics
unit_id = random.choice(list(topics_by_unit.keys()))
# Randomly select a topic from the unit
topic_id = random.choice(topics_by_unit[unit_id])
# Update the question
cursor.execute(
"UPDATE questions SET unit_id = ?, topic_id = ? WHERE id = ?",
(unit_id, topic_id, question_id)
)
updated_count += 1
# Commit changes
conn.commit()
print(f"Successfully updated {updated_count} questions with random units and topics.")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
if conn:
conn.close()
if __name__ == "__main__":
assign_units_topics()