Spaces:
Sleeping
Sleeping
Update database.py
Browse files- database.py +227 -100
database.py
CHANGED
|
@@ -1,100 +1,227 @@
|
|
| 1 |
-
import sqlite3
|
| 2 |
-
|
| 3 |
-
# Create and initialize the database
|
| 4 |
-
def initialize_database():
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# Function to execute SQL query and return results
|
| 78 |
-
def run_sql_query(sql_query):
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# Initialize database when this script runs directly
|
| 99 |
-
if __name__ == "__main__":
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_info 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 |
+
# );
|
| 47 |
+
# """)
|
| 48 |
+
|
| 49 |
+
# # Insert sample data into student table
|
| 50 |
+
# cursor.execute("DELETE FROM student;")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# cursor.executemany("INSERT OR IGNORE INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", [
|
| 55 |
+
# (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
|
| 56 |
+
# (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
|
| 57 |
+
# ])
|
| 58 |
+
|
| 59 |
+
# # Insert sample data into employee table
|
| 60 |
+
# cursor.execute("DELETE FROM employee;")
|
| 61 |
+
# cursor.executemany("INSERT OR IGNORE INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", [
|
| 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 |
+
# ])
|
| 65 |
+
|
| 66 |
+
# # Insert sample data into course_info table
|
| 67 |
+
# cursor.execute("DELETE FROM course;")
|
| 68 |
+
# cursor.executemany("INSERT OR IGNORE INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", [
|
| 69 |
+
# (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
|
| 70 |
+
# (2, "Digital Electronics", "EC202", 1, "ECE", 3, "Spring"),
|
| 71 |
+
# ])
|
| 72 |
+
|
| 73 |
+
# conn.commit()
|
| 74 |
+
# conn.close()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# # Function to execute SQL query and return results
|
| 78 |
+
# def run_sql_query(sql_query):
|
| 79 |
+
# try:
|
| 80 |
+
# conn = sqlite3.connect("college.db")
|
| 81 |
+
# cursor = conn.cursor()
|
| 82 |
+
# cursor.execute(sql_query)
|
| 83 |
+
# rows = cursor.fetchall()
|
| 84 |
+
# column_names = [description[0] for description in cursor.description] if cursor.description else []
|
| 85 |
+
# conn.commit()
|
| 86 |
+
# conn.close()
|
| 87 |
+
|
| 88 |
+
# if not rows:
|
| 89 |
+
# return "No rows returned."
|
| 90 |
+
|
| 91 |
+
# result = "\t".join(column_names) + "\n"
|
| 92 |
+
# for row in rows:
|
| 93 |
+
# result += "\t".join(str(cell) for cell in row) + "\n"
|
| 94 |
+
# return result.strip()
|
| 95 |
+
# except Exception as e:
|
| 96 |
+
# return f"Error executing query: {e}"
|
| 97 |
+
|
| 98 |
+
# # Initialize database when this script runs directly
|
| 99 |
+
# if __name__ == "__main__":
|
| 100 |
+
# initialize_database()
|
| 101 |
+
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 |
+
cursor.execute("DELETE FROM employee;")
|
| 156 |
+
cursor.execute("DELETE FROM course;")
|
| 157 |
+
|
| 158 |
+
# Insert 10 students
|
| 159 |
+
students = [
|
| 160 |
+
(1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
|
| 161 |
+
(2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
|
| 162 |
+
(3, "Charlie", "Brown", "2002-01-12", "charlie@example.com", "1111111111", "Electrical Engineering", 2020),
|
| 163 |
+
(4, "Daisy", "Miller", "2001-11-05", "daisy@example.com", "2222222222", "Electronics", 2020),
|
| 164 |
+
(5, "Ethan", "Hunt", "1999-09-30", "ethan@example.com", "3333333333", "Mechanical Engineering", 2017),
|
| 165 |
+
(6, "Fiona", "Gray", "2003-03-10", "fiona@example.com", "4444444444", "Civil Engineering", 2021),
|
| 166 |
+
(7, "George", "King", "2000-08-20", "george@example.com", "5555555555", "Computer Science", 2018),
|
| 167 |
+
(8, "Hannah", "Scott", "2001-02-25", "hannah@example.com", "6666666666", "Information Technology", 2019),
|
| 168 |
+
(9, "Ian", "Clark", "2002-07-07", "ian@example.com", "7777777777", "Electronics", 2020),
|
| 169 |
+
(10, "Julia", "Anderson", "1998-12-18", "julia@example.com", "8888888888", "Civil Engineering", 2016),
|
| 170 |
+
]
|
| 171 |
+
cursor.executemany("INSERT OR IGNORE INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", students)
|
| 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 |
+
|
| 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."
|
| 215 |
+
|
| 216 |
+
result = "\t".join(column_names) + "\n"
|
| 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 |
+
|