Balaprime commited on
Commit
e2b7d1d
·
verified ·
1 Parent(s): 9c525f8

Update src/database.py

Browse files
Files changed (1) hide show
  1. 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
- # Clear old data
47
- cursor.execute("DELETE FROM course;")
48
- cursor.execute("DELETE FROM student;")
49
- cursor.execute("DELETE FROM employee;")
50
 
51
  # Insert sample data
52
  students = [
53
- (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
54
- (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
55
- (3, "Charlie", "Brown", "2002-01-10", "charlie@example.com", "1112223333", "ECE", 2020),
56
- (4, "Daisy", "Miller", "2001-12-12", "daisy@example.com", "2223334444", "Civil", 2019),
57
- (5, "Ethan", "Lee", "2000-05-05", "ethan@example.com", "3334445555", "IT", 2018)
58
  ]
59
- cursor.executemany("INSERT INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", students)
60
 
61
  employees = [
62
- (1, "Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
63
- (2, "Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
64
- (3, "James", "Hall", "james@example.com", "IT", "Lecturer", 55000.00, "2019-01-15")
65
  ]
66
- cursor.executemany("INSERT INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", employees)
67
 
68
  courses = [
69
- (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
70
- (2, "Digital Electronics", "EC202", 2, "ECE", 3, "Spring"),
71
- (3, "Web Technologies", "IT301", 3, "IT", 4, "Spring")
72
  ]
73
- cursor.executemany("INSERT INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", courses)
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.")