Samuel Oberhofer commited on
Commit
a1e6f72
·
1 Parent(s): e6ae8f8

feat: Set up initial project structure and database script

Browse files
Files changed (3) hide show
  1. app.py +0 -0
  2. database/setup_db.py +87 -0
  3. requirements.txt +2 -1
app.py CHANGED
File without changes
database/setup_db.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ from faker import Faker
3
+
4
+ def setup_database():
5
+ """
6
+ Initializes and populates the SQLite database with synthetic data.
7
+ """
8
+ conn = sqlite3.connect('database/university.db')
9
+ cursor = conn.cursor()
10
+
11
+ # Create tables
12
+ cursor.execute('''
13
+ CREATE TABLE IF NOT EXISTS students (
14
+ id INTEGER PRIMARY KEY,
15
+ name TEXT NOT NULL,
16
+ email TEXT NOT NULL UNIQUE
17
+ )
18
+ ''')
19
+
20
+ cursor.execute('''
21
+ CREATE TABLE IF NOT EXISTS faculty (
22
+ id INTEGER PRIMARY KEY,
23
+ name TEXT NOT NULL,
24
+ email TEXT NOT NULL UNIQUE,
25
+ department TEXT NOT NULL
26
+ )
27
+ ''')
28
+
29
+ cursor.execute('''
30
+ CREATE TABLE IF NOT EXISTS courses (
31
+ id INTEGER PRIMARY KEY,
32
+ name TEXT NOT NULL,
33
+ faculty_id INTEGER,
34
+ FOREIGN KEY (faculty_id) REFERENCES faculty (id)
35
+ )
36
+ ''')
37
+
38
+ cursor.execute('''
39
+ CREATE TABLE IF NOT EXISTS enrollments (
40
+ student_id INTEGER,
41
+ course_id INTEGER,
42
+ PRIMARY KEY (student_id, course_id),
43
+ FOREIGN KEY (student_id) REFERENCES students (id),
44
+ FOREIGN KEY (course_id) REFERENCES courses (id)
45
+ )
46
+ ''')
47
+
48
+ # Populate with synthetic data
49
+ fake = Faker()
50
+
51
+ # Add students
52
+ for _ in range(500):
53
+ try:
54
+ cursor.execute("INSERT INTO students (name, email) VALUES (?, ?)", (fake.name(), fake.email()))
55
+ except sqlite3.IntegrityError:
56
+ pass
57
+
58
+ # Add faculty
59
+ for _ in range(100):
60
+ try:
61
+ cursor.execute("INSERT INTO faculty (name, email, department) VALUES (?, ?, ?)",
62
+ (fake.name(), fake.email(), fake.job()))
63
+ except sqlite3.IntegrityError:
64
+ pass
65
+
66
+ # Add courses
67
+ faculty_ids = [row[0] for row in cursor.execute("SELECT id FROM faculty").fetchall()]
68
+ for _ in range(200):
69
+ cursor.execute("INSERT INTO courses (name, faculty_id) VALUES (?, ?)",
70
+ (fake.bs(), fake.random_element(elements=faculty_ids)))
71
+
72
+ # Add enrollments
73
+ student_ids = [row[0] for row in cursor.execute("SELECT id FROM students").fetchall()]
74
+ course_ids = [row[0] for row in cursor.execute("SELECT id FROM courses").fetchall()]
75
+ for _ in range(1500):
76
+ try:
77
+ cursor.execute("INSERT INTO enrollments (student_id, course_id) VALUES (?, ?)",
78
+ (fake.random_element(elements=student_ids), fake.random_element(elements=course_ids)))
79
+ except sqlite3.IntegrityError:
80
+ pass
81
+
82
+ conn.commit()
83
+ conn.close()
84
+ print("Database setup complete. 'university.db' created and populated.")
85
+
86
+ if __name__ == "__main__":
87
+ setup_database()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  streamlit==1.37.0
2
- python-dotenv==1.0.1
 
 
1
  streamlit==1.37.0
2
+ python-dotenv==1.0.1
3
+ Faker==15.3.4