Balaprime commited on
Commit
1941ae5
·
verified ·
1 Parent(s): 0968362

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +107 -105
database.py CHANGED
@@ -102,113 +102,114 @@ import sqlite3
102
 
103
  # Create and initialize the database
104
  def initialize_database():
105
- with sqlite3.connect("college.db") as conn:
106
- cursor = conn.cursor()
107
-
108
- # Enable foreign key support
109
- cursor.execute("PRAGMA foreign_keys = ON;")
110
-
111
- # Create student table
112
- cursor.execute("""
113
- CREATE TABLE IF NOT EXISTS student (
114
- student_id INTEGER PRIMARY KEY,
115
- first_name TEXT,
116
- last_name TEXT,
117
- date_of_birth DATE,
118
- email TEXT,
119
- phone_number TEXT,
120
- major TEXT,
121
- year_of_enrollment INTEGER
122
- );
123
- """)
124
-
125
- # Create employee table
126
- cursor.execute("""
127
- CREATE TABLE IF NOT EXISTS employee (
128
- employee_id INTEGER PRIMARY KEY,
129
- first_name TEXT,
130
- last_name TEXT,
131
- email TEXT,
132
- department TEXT,
133
- position TEXT,
134
- salary REAL,
135
- date_of_joining DATE
136
- );
137
- """)
138
-
139
- # Create course table with foreign key
140
- cursor.execute("""
141
- CREATE TABLE IF NOT EXISTS course (
142
- course_id INTEGER PRIMARY KEY,
143
- course_name TEXT,
144
- course_code TEXT,
145
- instructor_id INTEGER,
146
- department TEXT,
147
- credits INTEGER,
148
- semester TEXT,
149
- FOREIGN KEY (instructor_id) REFERENCES employee(employee_id)
150
- );
151
- """)
152
-
153
- # Clear existing data
154
- cursor.execute("DELETE FROM student;")
155
-
156
-
157
- # Insert 10 students
158
- students = [
159
- (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
160
- (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
161
- (3, "Charlie", "Brown", "2002-01-12", "charlie@example.com", "1111111111", "Electrical Engineering", 2020),
162
- (4, "Daisy", "Miller", "2001-11-05", "daisy@example.com", "2222222222", "Electronics", 2020),
163
- (5, "Ethan", "Hunt", "1999-09-30", "ethan@example.com", "3333333333", "Mechanical Engineering", 2017),
164
- (6, "Fiona", "Gray", "2003-03-10", "fiona@example.com", "4444444444", "Civil Engineering", 2021),
165
- (7, "George", "King", "2000-08-20", "george@example.com", "5555555555", "Computer Science", 2018),
166
- (8, "Hannah", "Scott", "2001-02-25", "hannah@example.com", "6666666666", "Information Technology", 2019),
167
- (9, "Ian", "Clark", "2002-07-07", "ian@example.com", "7777777777", "Electronics", 2020),
168
- (10, "Julia", "Anderson", "1998-12-18", "julia@example.com", "8888888888", "Civil Engineering", 2016),
169
- ]
170
- cursor.executemany("INSERT OR IGNORE INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", students)
171
- cursor.execute("DELETE FROM employee;")
172
-
173
- # Insert 10 employees
174
- employees = [
175
- (1, "Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
176
- (2, "Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
177
- (3, "Nina", "Gomes", "nina@example.com", "IT", "Lecturer", 55000.00, "2020-01-15"),
178
- (4, "Oscar", "Fernandez", "oscar@example.com", "ME", "Professor", 80000.00, "2014-03-12"),
179
- (5, "Priya", "Singh", "priya@example.com", "CSE", "Assistant Professor", 62000.00, "2019-06-01"),
180
- (6, "Rahul", "Kumar", "rahul@example.com", "EEE", "Lecturer", 50000.00, "2021-09-20"),
181
- (7, "Sana", "Shaikh", "sana@example.com", "CIV", "Lecturer", 52000.00, "2022-02-11"),
182
- (8, "Tom", "Andrews", "tom@example.com", "ECE", "Associate Professor", 70000.00, "2016-12-09"),
183
- (9, "Uma", "Joshi", "uma@example.com", "IT", "Professor", 78000.00, "2013-05-25"),
184
- (10, "Vikram", "Reddy", "vikram@example.com", "ME", "Assistant Professor", 60000.00, "2017-11-03"),
185
- ]
186
- cursor.executemany("INSERT OR IGNORE INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", employees)
187
- cursor.execute("DELETE FROM course;")
188
- # Insert 10 courses (linked to instructors from employee table)
189
- courses = [
190
- (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
191
- (2, "Digital Electronics", "EC202", 2, "ECE", 3, "Spring"),
192
- (3, "Operating Systems", "CS302", 1, "CSE", 4, "Spring"),
193
- (4, "Thermodynamics", "ME101", 4, "ME", 3, "Fall"),
194
- (5, "Signals and Systems", "EC301", 2, "ECE", 3, "Fall"),
195
- (6, "Data Structures", "CS201", 5, "CSE", 4, "Spring"),
196
- (7, "Electrical Machines", "EE101", 6, "EEE", 4, "Fall"),
197
- (8, "Structural Analysis", "CIV301", 7, "CIV", 3, "Spring"),
198
- (9, "Web Technologies", "IT401", 3, "IT", 3, "Fall"),
199
- (10, "Fluid Mechanics", "ME203", 10, "ME", 4, "Spring"),
200
- ]
201
- cursor.executemany("INSERT OR IGNORE INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", courses)
202
 
203
  # Function to execute SQL query and return results
204
  def run_sql_query(sql_query):
205
  try:
206
- with sqlite3.connect("college.db") as conn:
207
- cursor = conn.cursor()
208
- cursor.execute("PRAGMA foreign_keys = ON;")
209
- cursor.execute(sql_query)
210
- rows = cursor.fetchall()
211
- column_names = [description[0] for description in cursor.description] if cursor.description else []
 
212
 
213
  if not rows:
214
  return "No rows returned."
@@ -217,11 +218,12 @@ def run_sql_query(sql_query):
217
  for row in rows:
218
  result += "\t".join(str(cell) for cell in row) + "\n"
219
  return result.strip()
220
-
221
  except Exception as e:
222
  return f"Error executing query: {e}"
223
 
224
- # Initialize database when this script runs directly
225
  if __name__ == "__main__":
226
  initialize_database()
 
 
227
 
 
102
 
103
  # Create and initialize the database
104
  def initialize_database():
105
+ conn = sqlite3.connect("college.db")
106
+ cursor = conn.cursor()
107
+
108
+ # Create student table
109
+ cursor.execute("""
110
+ CREATE TABLE IF NOT EXISTS student (
111
+ student_id INTEGER PRIMARY KEY,
112
+ first_name TEXT,
113
+ last_name TEXT,
114
+ date_of_birth DATE,
115
+ email TEXT,
116
+ phone_number TEXT,
117
+ major TEXT,
118
+ year_of_enrollment INTEGER
119
+ );
120
+ """)
121
+
122
+ # Create employee table
123
+ cursor.execute("""
124
+ CREATE TABLE IF NOT EXISTS employee (
125
+ employee_id INTEGER PRIMARY KEY,
126
+ first_name TEXT,
127
+ last_name TEXT,
128
+ email TEXT,
129
+ department TEXT,
130
+ position TEXT,
131
+ salary REAL,
132
+ date_of_joining DATE
133
+ );
134
+ """)
135
+
136
+ # Create course table
137
+ cursor.execute("""
138
+ CREATE TABLE IF NOT EXISTS course (
139
+ course_id INTEGER PRIMARY KEY,
140
+ course_name TEXT,
141
+ course_code TEXT,
142
+ instructor_id INTEGER,
143
+ department TEXT,
144
+ credits INTEGER,
145
+ semester TEXT,
146
+ FOREIGN KEY (instructor_id) REFERENCES employee(employee_id)
147
+ );
148
+ """)
149
+
150
+ # Clear old data (order matters to avoid foreign key errors)
151
+ cursor.execute("DELETE FROM course;")
152
+ cursor.execute("DELETE FROM student;")
153
+ cursor.execute("DELETE FROM employee;")
154
+
155
+ # Insert sample data into student table
156
+ students = [
157
+ (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
158
+ (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
159
+ (3, "Charlie", "Brown", "2002-01-10", "charlie@example.com", "1112223333", "ECE", 2020),
160
+ (4, "Daisy", "Miller", "2001-12-12", "daisy@example.com", "2223334444", "Civil", 2019),
161
+ (5, "Ethan", "Lee", "2000-05-05", "ethan@example.com", "3334445555", "IT", 2018),
162
+ (6, "Fiona", "Clark", "2001-07-07", "fiona@example.com", "4445556666", "CSE", 2020),
163
+ (7, "George", "Adams", "2002-02-02", "george@example.com", "5556667777", "CSE", 2021),
164
+ (8, "Hannah", "Moore", "2001-11-11", "hannah@example.com", "6667778888", "AI", 2019),
165
+ (9, "Ivan", "Davis", "1999-09-09", "ivan@example.com", "7778889999", "ECE", 2017),
166
+ (10, "Julia", "King", "2002-03-03", "julia@example.com", "8889990000", "IT", 2020)
167
+ ]
168
+ cursor.executemany("INSERT INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", students)
169
+
170
+ # Insert sample data into employee table
171
+ employees = [
172
+ (1, "Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
173
+ (2, "Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
174
+ (3, "James", "Hall", "james@example.com", "IT", "Lecturer", 55000.00, "2019-01-15"),
175
+ (4, "Nancy", "Allen", "nancy@example.com", "Civil", "Professor", 80000.00, "2014-06-12"),
176
+ (5, "Oscar", "Wright", "oscar@example.com", "Mechanical", "Assistant Professor", 60000.00, "2016-10-01"),
177
+ (6, "Rachel", "Baker", "rachel@example.com", "AI", "Lecturer", 52000.00, "2020-03-05"),
178
+ (7, "Steve", "Lopez", "steve@example.com", "CSE", "Professor", 78000.00, "2012-08-20"),
179
+ (8, "Tina", "Hill", "tina@example.com", "ECE", "Lecturer", 53000.00, "2017-09-18"),
180
+ (9, "Uma", "Scott", "uma@example.com", "IT", "Professor", 72000.00, "2013-04-07"),
181
+ (10, "Victor", "Green", "victor@example.com", "CSE", "Assistant Professor", 60000.00, "2018-11-23")
182
+ ]
183
+ cursor.executemany("INSERT INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", employees)
184
+
185
+ # Insert sample data into course table
186
+ courses = [
187
+ (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
188
+ (2, "Digital Electronics", "EC202", 2, "ECE", 3, "Spring"),
189
+ (3, "Operating Systems", "CS302", 1, "CSE", 4, "Fall"),
190
+ (4, "Data Structures", "CS201", 10, "CSE", 4, "Spring"),
191
+ (5, "Microprocessors", "EC303", 8, "ECE", 3, "Fall"),
192
+ (6, "Civil Engineering Basics", "CV101", 4, "Civil", 3, "Spring"),
193
+ (7, "Thermodynamics", "ME201", 5, "Mechanical", 4, "Fall"),
194
+ (8, "AI Fundamentals", "AI101", 6, "AI", 3, "Fall"),
195
+ (9, "Web Technologies", "IT301", 3, "IT", 4, "Spring"),
196
+ (10, "Software Engineering", "CS303", 7, "CSE", 4, "Fall")
197
+ ]
198
+ cursor.executemany("INSERT INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", courses)
199
+
200
+ conn.commit()
201
+ conn.close()
202
 
203
  # Function to execute SQL query and return results
204
  def run_sql_query(sql_query):
205
  try:
206
+ conn = sqlite3.connect("college.db")
207
+ cursor = conn.cursor()
208
+ cursor.execute(sql_query)
209
+ rows = cursor.fetchall()
210
+ column_names = [description[0] for description in cursor.description] if cursor.description else []
211
+ conn.commit()
212
+ conn.close()
213
 
214
  if not rows:
215
  return "No rows returned."
 
218
  for row in rows:
219
  result += "\t".join(str(cell) for cell in row) + "\n"
220
  return result.strip()
 
221
  except Exception as e:
222
  return f"Error executing query: {e}"
223
 
224
+ # Run only if this script is the main entry point
225
  if __name__ == "__main__":
226
  initialize_database()
227
+
228
+
229