File size: 8,482 Bytes
be63f4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1941ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be63f4e
 
 
 
1941ae5
 
 
 
 
 
 
be63f4e
 
 
 
 
 
 
 
 
 
 
1941ae5
be63f4e
 
1941ae5
 
be63f4e
1
2
3
4
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# import sqlite3

# # Create and initialize the database
# def initialize_database():
#     conn = sqlite3.connect("college.db")
#     cursor = conn.cursor()

#     # Create student table
#     cursor.execute("""
#     CREATE TABLE IF NOT EXISTS student (
#         student_id INTEGER PRIMARY KEY,
#         first_name TEXT,
#         last_name TEXT,
#         date_of_birth DATE,
#         email TEXT,
#         phone_number TEXT,
#         major TEXT,
#         year_of_enrollment INTEGER
#     );
#     """)

#     # Create employee table
#     cursor.execute("""
#     CREATE TABLE IF NOT EXISTS employee (
#         employee_id INTEGER PRIMARY KEY,
#         first_name TEXT,
#         last_name TEXT,
#         email TEXT,
#         department TEXT,
#         position TEXT,
#         salary REAL,
#         date_of_joining DATE
#     );
#     """)

#     # Create course_info table
#     cursor.execute("""
#     CREATE TABLE IF NOT EXISTS course (
#         course_id INTEGER PRIMARY KEY,
#         course_name TEXT,
#         course_code TEXT,
#         instructor_id INTEGER,
#         department TEXT,
#         credits INTEGER,
#         semester TEXT
#     );
#     """)

#     # Insert sample data into student table
#     cursor.execute("DELETE FROM student;")



#     cursor.executemany("INSERT OR IGNORE INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", [
#         (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
#         (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
#     ])

#     # Insert sample data into employee table
#     cursor.execute("DELETE FROM employee;")
#     cursor.executemany("INSERT OR IGNORE INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", [
#         (1, "Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
#         (2, "Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
#     ])

#     # Insert sample data into course_info table
#     cursor.execute("DELETE FROM course;")
#     cursor.executemany("INSERT OR IGNORE INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", [
#         (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
#         (2, "Digital Electronics", "EC202", 1, "ECE", 3, "Spring"),
#     ])

#     conn.commit()
#     conn.close()


# # Function to execute SQL query and return results
# def run_sql_query(sql_query):
#     try:
#         conn = sqlite3.connect("college.db")
#         cursor = conn.cursor()
#         cursor.execute(sql_query)
#         rows = cursor.fetchall()
#         column_names = [description[0] for description in cursor.description] if cursor.description else []
#         conn.commit()
#         conn.close()

#         if not rows:
#             return "No rows returned."

#         result = "\t".join(column_names) + "\n"
#         for row in rows:
#             result += "\t".join(str(cell) for cell in row) + "\n"
#         return result.strip()
#     except Exception as e:
#         return f"Error executing query: {e}"

# # Initialize database when this script runs directly
# if __name__ == "__main__":
#     initialize_database()
import sqlite3

# Create and initialize the database
def initialize_database():
    conn = sqlite3.connect("college.db")
    cursor = conn.cursor()

    # Create student table
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS student (
        student_id INTEGER PRIMARY KEY,
        first_name TEXT,
        last_name TEXT,
        date_of_birth DATE,
        email TEXT,
        phone_number TEXT,
        major TEXT,
        year_of_enrollment INTEGER
    );
    """)

    # Create employee table
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS employee (
        employee_id INTEGER PRIMARY KEY,
        first_name TEXT,
        last_name TEXT,
        email TEXT,
        department TEXT,
        position TEXT,
        salary REAL,
        date_of_joining DATE
    );
    """)

    # Create course table
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS course (
        course_id INTEGER PRIMARY KEY,
        course_name TEXT,
        course_code TEXT,
        instructor_id INTEGER,
        department TEXT,
        credits INTEGER,
        semester TEXT,
        FOREIGN KEY (instructor_id) REFERENCES employee(employee_id)
    );
    """)

    # Clear old data (order matters to avoid foreign key errors)
    cursor.execute("DELETE FROM course;")
    cursor.execute("DELETE FROM student;")
    cursor.execute("DELETE FROM employee;")

    # Insert sample data into student table
    students = [
        (1, "Alice", "Johnson", "2001-06-15", "alice@example.com", "1234567890", "Computer Science", 2019),
        (2, "Bob", "Smith", "2000-04-22", "bob@example.com", "0987654321", "Mechanical Engineering", 2018),
        (3, "Charlie", "Brown", "2002-01-10", "charlie@example.com", "1112223333", "ECE", 2020),
        (4, "Daisy", "Miller", "2001-12-12", "daisy@example.com", "2223334444", "Civil", 2019),
        (5, "Ethan", "Lee", "2000-05-05", "ethan@example.com", "3334445555", "IT", 2018),
        (6, "Fiona", "Clark", "2001-07-07", "fiona@example.com", "4445556666", "CSE", 2020),
        (7, "George", "Adams", "2002-02-02", "george@example.com", "5556667777", "CSE", 2021),
        (8, "Hannah", "Moore", "2001-11-11", "hannah@example.com", "6667778888", "AI", 2019),
        (9, "Ivan", "Davis", "1999-09-09", "ivan@example.com", "7778889999", "ECE", 2017),
        (10, "Julia", "King", "2002-03-03", "julia@example.com", "8889990000", "IT", 2020)
    ]
    cursor.executemany("INSERT INTO student VALUES (?, ?, ?, ?, ?, ?, ?, ?);", students)

    # Insert sample data into employee table
    employees = [
        (1, "Drake", "Miller", "drake@example.com", "CSE", "Professor", 75000.00, "2015-08-01"),
        (2, "Laura", "White", "laura@example.com", "ECE", "Assistant Professor", 65000.00, "2018-07-10"),
        (3, "James", "Hall", "james@example.com", "IT", "Lecturer", 55000.00, "2019-01-15"),
        (4, "Nancy", "Allen", "nancy@example.com", "Civil", "Professor", 80000.00, "2014-06-12"),
        (5, "Oscar", "Wright", "oscar@example.com", "Mechanical", "Assistant Professor", 60000.00, "2016-10-01"),
        (6, "Rachel", "Baker", "rachel@example.com", "AI", "Lecturer", 52000.00, "2020-03-05"),
        (7, "Steve", "Lopez", "steve@example.com", "CSE", "Professor", 78000.00, "2012-08-20"),
        (8, "Tina", "Hill", "tina@example.com", "ECE", "Lecturer", 53000.00, "2017-09-18"),
        (9, "Uma", "Scott", "uma@example.com", "IT", "Professor", 72000.00, "2013-04-07"),
        (10, "Victor", "Green", "victor@example.com", "CSE", "Assistant Professor", 60000.00, "2018-11-23")
    ]
    cursor.executemany("INSERT INTO employee VALUES (?, ?, ?, ?, ?, ?, ?, ?);", employees)

    # Insert sample data into course table
    courses = [
        (1, "Database Systems", "CS301", 1, "CSE", 4, "Fall"),
        (2, "Digital Electronics", "EC202", 2, "ECE", 3, "Spring"),
        (3, "Operating Systems", "CS302", 1, "CSE", 4, "Fall"),
        (4, "Data Structures", "CS201", 10, "CSE", 4, "Spring"),
        (5, "Microprocessors", "EC303", 8, "ECE", 3, "Fall"),
        (6, "Civil Engineering Basics", "CV101", 4, "Civil", 3, "Spring"),
        (7, "Thermodynamics", "ME201", 5, "Mechanical", 4, "Fall"),
        (8, "AI Fundamentals", "AI101", 6, "AI", 3, "Fall"),
        (9, "Web Technologies", "IT301", 3, "IT", 4, "Spring"),
        (10, "Software Engineering", "CS303", 7, "CSE", 4, "Fall")
    ]
    cursor.executemany("INSERT INTO course VALUES (?, ?, ?, ?, ?, ?, ?);", courses)

    conn.commit()
    conn.close()

# Function to execute SQL query and return results
def run_sql_query(sql_query):
    try:
        conn = sqlite3.connect("college.db")
        cursor = conn.cursor()
        cursor.execute(sql_query)
        rows = cursor.fetchall()
        column_names = [description[0] for description in cursor.description] if cursor.description else []
        conn.commit()
        conn.close()

        if not rows:
            return "No rows returned."

        result = "\t".join(column_names) + "\n"
        for row in rows:
            result += "\t".join(str(cell) for cell in row) + "\n"
        return result.strip()
    except Exception as e:
        return f"Error executing query: {e}"

# Run only if this script is the main entry point
if __name__ == "__main__":
    initialize_database()