Spaces:
Build error
Build error
Update src/database.py
Browse files- src/database.py +25 -23
src/database.py
CHANGED
|
@@ -1,15 +1,16 @@
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
def initialize_database():
|
| 5 |
-
db_path = os.path.join(os.path.dirname(__file__), "../college.db")
|
|
|
|
| 6 |
conn = sqlite3.connect(db_path)
|
| 7 |
cursor = conn.cursor()
|
| 8 |
|
| 9 |
# Create tables
|
| 10 |
cursor.execute("""
|
| 11 |
CREATE TABLE IF NOT EXISTS student (
|
| 12 |
-
student_id INTEGER PRIMARY KEY,
|
| 13 |
first_name TEXT,
|
| 14 |
last_name TEXT,
|
| 15 |
date_of_birth DATE,
|
|
@@ -21,7 +22,7 @@ def initialize_database():
|
|
| 21 |
|
| 22 |
cursor.execute("""
|
| 23 |
CREATE TABLE IF NOT EXISTS employee (
|
| 24 |
-
employee_id INTEGER PRIMARY KEY,
|
| 25 |
first_name TEXT,
|
| 26 |
last_name TEXT,
|
| 27 |
email TEXT,
|
|
@@ -33,7 +34,7 @@ def initialize_database():
|
|
| 33 |
|
| 34 |
cursor.execute("""
|
| 35 |
CREATE TABLE IF NOT EXISTS course (
|
| 36 |
-
course_id INTEGER PRIMARY KEY,
|
| 37 |
course_name TEXT,
|
| 38 |
course_code TEXT,
|
| 39 |
instructor_id INTEGER,
|
|
@@ -43,34 +44,35 @@ def initialize_database():
|
|
| 43 |
FOREIGN KEY (instructor_id) REFERENCES employee(employee_id)
|
| 44 |
);""")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
# Insert sample data
|
| 52 |
students = [
|
| 53 |
-
(
|
| 54 |
-
(
|
| 55 |
-
(
|
| 56 |
-
(
|
| 57 |
-
(
|
| 58 |
]
|
| 59 |
-
cursor.executemany("INSERT INTO student VALUES (?, ?, ?, ?, ?, ?, ?
|
| 60 |
|
| 61 |
employees = [
|
| 62 |
-
(
|
| 63 |
-
(
|
| 64 |
-
(
|
| 65 |
]
|
| 66 |
-
cursor.executemany("INSERT INTO employee VALUES (?, ?, ?, ?, ?, ?, ?
|
| 67 |
|
| 68 |
courses = [
|
| 69 |
-
(
|
| 70 |
-
(
|
| 71 |
-
(
|
| 72 |
]
|
| 73 |
-
cursor.executemany("INSERT INTO course VALUES (?, ?, ?, ?, ?, ?
|
| 74 |
|
| 75 |
conn.commit()
|
| 76 |
conn.close()
|
|
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
def initialize_database(clear_data=False):
|
| 5 |
+
db_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../college.db"))
|
| 6 |
+
print(f"Initializing database at: {db_path}")
|
| 7 |
conn = sqlite3.connect(db_path)
|
| 8 |
cursor = conn.cursor()
|
| 9 |
|
| 10 |
# Create tables
|
| 11 |
cursor.execute("""
|
| 12 |
CREATE TABLE IF NOT EXISTS student (
|
| 13 |
+
student_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
first_name TEXT,
|
| 15 |
last_name TEXT,
|
| 16 |
date_of_birth DATE,
|
|
|
|
| 22 |
|
| 23 |
cursor.execute("""
|
| 24 |
CREATE TABLE IF NOT EXISTS employee (
|
| 25 |
+
employee_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 26 |
first_name TEXT,
|
| 27 |
last_name TEXT,
|
| 28 |
email TEXT,
|
|
|
|
| 34 |
|
| 35 |
cursor.execute("""
|
| 36 |
CREATE TABLE IF NOT EXISTS course (
|
| 37 |
+
course_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 38 |
course_name TEXT,
|
| 39 |
course_code TEXT,
|
| 40 |
instructor_id INTEGER,
|
|
|
|
| 44 |
FOREIGN KEY (instructor_id) REFERENCES employee(employee_id)
|
| 45 |
);""")
|
| 46 |
|
| 47 |
+
if clear_data:
|
| 48 |
+
cursor.execute("DELETE FROM course;")
|
| 49 |
+
cursor.execute("DELETE FROM student;")
|
| 50 |
+
cursor.execute("DELETE FROM employee;")
|
| 51 |
|
| 52 |
# Insert sample data
|
| 53 |
students = [
|
| 54 |
+
("Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
|
| 55 |
+
("Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
|
| 56 |
+
("Charlie", "Brown", "2002-01-10", "charlie@example.com", "1112223333", "ECE", 2020),
|
| 57 |
+
("Daisy", "Miller", "2001-12-12", "daisy@example.com", "2223334444", "Civil", 2019),
|
| 58 |
+
("Ethan", "Lee", "2000-05-05", "ethan@example.com", "3334445555", "IT", 2018)
|
| 59 |
]
|
| 60 |
+
cursor.executemany("INSERT INTO student (first_name, last_name, date_of_birth, email, phone_number, major, year_of_enrollment) VALUES (?, ?, ?, ?, ?, ?, ?);", students)
|
| 61 |
|
| 62 |
employees = [
|
| 63 |
+
("Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
|
| 64 |
+
("Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
|
| 65 |
+
("James", "Hall", "james@example.com", "IT", "Lecturer", 55000.00, "2019-01-15")
|
| 66 |
]
|
| 67 |
+
cursor.executemany("INSERT INTO employee (first_name, last_name, email, department, position, salary, date_of_joining) VALUES (?, ?, ?, ?, ?, ?, ?);", employees)
|
| 68 |
|
| 69 |
courses = [
|
| 70 |
+
("Database Systems", "CS301", 1, "CSE", 4, "Fall"),
|
| 71 |
+
("Digital Electronics", "EC202", 2, "ECE", 3, "Spring"),
|
| 72 |
+
("Web Technologies", "IT301", 3, "IT", 4, "Spring")
|
| 73 |
]
|
| 74 |
+
cursor.executemany("INSERT INTO course (course_name, course_code, instructor_id, department, credits, semester) VALUES (?, ?, ?, ?, ?, ?);", courses)
|
| 75 |
|
| 76 |
conn.commit()
|
| 77 |
conn.close()
|
| 78 |
+
print("✅ Database initialized and sample data inserted.")
|