Balaprime commited on
Commit
77560cb
·
verified ·
1 Parent(s): b1f78f1

Create database.py

Browse files
Files changed (1) hide show
  1. database.py +126 -0
database.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ # Create and initialize the database
4
+ def initialize_database():
5
+ conn = sqlite3.connect("college.db")
6
+ cursor = conn.cursor()
7
+
8
+ # Create student table
9
+ cursor.execute("""
10
+ CREATE TABLE IF NOT EXISTS student (
11
+ student_id INTEGER PRIMARY KEY,
12
+ first_name TEXT,
13
+ last_name TEXT,
14
+ date_of_birth DATE,
15
+ email TEXT,
16
+ phone_number TEXT,
17
+ major TEXT,
18
+ year_of_enrollment INTEGER
19
+ );
20
+ """)
21
+
22
+ # Create employee table
23
+ cursor.execute("""
24
+ CREATE TABLE IF NOT EXISTS employee (
25
+ employee_id INTEGER PRIMARY KEY,
26
+ first_name TEXT,
27
+ last_name TEXT,
28
+ email TEXT,
29
+ department TEXT,
30
+ position TEXT,
31
+ salary REAL,
32
+ date_of_joining DATE
33
+ );
34
+ """)
35
+
36
+ # Create course table
37
+ cursor.execute("""
38
+ CREATE TABLE IF NOT EXISTS course (
39
+ course_id INTEGER PRIMARY KEY,
40
+ course_name TEXT,
41
+ course_code TEXT,
42
+ instructor_id INTEGER,
43
+ department TEXT,
44
+ credits INTEGER,
45
+ semester TEXT,
46
+ FOREIGN KEY (instructor_id) REFERENCES employee(employee_id)
47
+ );
48
+ """)
49
+
50
+ # Clear old data (order matters to avoid foreign key errors)
51
+ cursor.execute("DELETE FROM course;")
52
+ cursor.execute("DELETE FROM student;")
53
+ cursor.execute("DELETE FROM employee;")
54
+
55
+ # Insert sample data into student table
56
+ students = [
57
+ (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
58
+ (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
59
+ (3, "Charlie", "Brown", "2002-01-10", "charlie@example.com", "1112223333", "ECE", 2020),
60
+ (4, "Daisy", "Miller", "2001-12-12", "daisy@example.com", "2223334444", "Civil", 2019),
61
+ (5, "Ethan", "Lee", "2000-05-05", "ethan@example.com", "3334445555", "IT", 2018),
62
+ (6, "Fiona", "Clark", "2001-07-07", "fiona@example.com", "4445556666", "CSE", 2020),
63
+ (7, "George", "Adams", "2002-02-02", "george@example.com", "5556667777", "CSE", 2021),
64
+ (8, "Hannah", "Moore", "2001-11-11", "hannah@example.com", "6667778888", "AI", 2019),
65
+ (9, "Ivan", "Davis", "1999-09-09", "ivan@example.com", "7778889999", "ECE", 2017),
66
+ (10, "Julia", "King", "2002-03-03", "julia@example.com", "8889990000", "IT", 2020)
67
+ ]
68
+ cursor.executemany("INSERT INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", students)
69
+
70
+ # Insert sample data into employee table
71
+ employees = [
72
+ (1, "Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
73
+ (2, "Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
74
+ (3, "James", "Hall", "james@example.com", "IT", "Lecturer", 55000.00, "2019-01-15"),
75
+ (4, "Nancy", "Allen", "nancy@example.com", "Civil", "Professor", 80000.00, "2014-06-12"),
76
+ (5, "Oscar", "Wright", "oscar@example.com", "Mechanical", "Assistant Professor", 60000.00, "2016-10-01"),
77
+ (6, "Rachel", "Baker", "rachel@example.com", "AI", "Lecturer", 52000.00, "2020-03-05"),
78
+ (7, "Steve", "Lopez", "steve@example.com", "CSE", "Professor", 78000.00, "2012-08-20"),
79
+ (8, "Tina", "Hill", "tina@example.com", "ECE", "Lecturer", 53000.00, "2017-09-18"),
80
+ (9, "Uma", "Scott", "uma@example.com", "IT", "Professor", 72000.00, "2013-04-07"),
81
+ (10, "Victor", "Green", "victor@example.com", "CSE", "Assistant Professor", 60000.00, "2018-11-23")
82
+ ]
83
+ cursor.executemany("INSERT INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", employees)
84
+
85
+ # Insert sample data into course table
86
+ courses = [
87
+ (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
88
+ (2, "Digital Electronics", "EC202", 2, "ECE", 3, "Spring"),
89
+ (3, "Operating Systems", "CS302", 1, "CSE", 4, "Fall"),
90
+ (4, "Data Structures", "CS201", 10, "CSE", 4, "Spring"),
91
+ (5, "Microprocessors", "EC303", 8, "ECE", 3, "Fall"),
92
+ (6, "Civil Engineering Basics", "CV101", 4, "Civil", 3, "Spring"),
93
+ (7, "Thermodynamics", "ME201", 5, "Mechanical", 4, "Fall"),
94
+ (8, "AI Fundamentals", "AI101", 6, "AI", 3, "Fall"),
95
+ (9, "Web Technologies", "IT301", 3, "IT", 4, "Spring"),
96
+ (10, "Software Engineering", "CS303", 7, "CSE", 4, "Fall")
97
+ ]
98
+ cursor.executemany("INSERT INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", courses)
99
+
100
+ conn.commit()
101
+ conn.close()
102
+
103
+ # Function to execute SQL query and return results
104
+ def run_sql_query(sql_query):
105
+ try:
106
+ conn = sqlite3.connect("college.db")
107
+ cursor = conn.cursor()
108
+ cursor.execute(sql_query)
109
+ rows = cursor.fetchall()
110
+ column_names = [description[0] for description in cursor.description] if cursor.description else []
111
+ conn.commit()
112
+ conn.close()
113
+
114
+ if not rows:
115
+ return "No rows returned."
116
+
117
+ result = "\t".join(column_names) + "\n"
118
+ for row in rows:
119
+ result += "\t".join(str(cell) for cell in row) + "\n"
120
+ return result.strip()
121
+ except Exception as e:
122
+ return f"Error executing query: {e}"
123
+
124
+ # Run only if this script is the main entry point
125
+ if __name__ == "__main__":
126
+ initialize_database()