Source code for a basic School Management System in Python using MySQL
Source code for a basic School Management System in Python using MySQL
This
code includes creating, updating, deleting, and viewing records for students and teachers.
---
Prerequisites:
---
import mysql.connector
# Database connection
def connect_to_database():
return mysql.connector.connect(
host="localhost",
user="root", # Replace with your MySQL username
password="password", # Replace with your MySQL password
database="school_management" # Replace with your database name
)
# Main menu
def main_menu():
print("\n--- School Management System ---")
print("1. Add Student")
print("2. View Students")
print("3. Update Student")
print("4. Delete Student")
print("5. Add Teacher")
print("6. View Teachers")
print("7. Exit")
choice = input("Enter your choice: ")
return choice
# Add student
def add_student():
db = connect_to_database()
cursor = db.cursor()
query = """
INSERT INTO Students (FirstName, LastName, RollNumber, ClassID, DateOfBirth, Gender,
Address, PhoneNumber, Email)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);
"""
cursor.execute(query, (first_name, last_name, roll_number, class_id, dob, gender, address,
phone, email))
db.commit()
print("Student added successfully.")
db.close()
# View students
def view_students():
db = connect_to_database()
cursor = db.cursor()
cursor.execute("SELECT * FROM Students;")
rows = cursor.fetchall()
print("\n--- Student Records ---")
for row in rows:
print(row)
db.close()
# Update student
def update_student():
db = connect_to_database()
cursor = db.cursor()
student_id = input("Enter Student ID to update: ")
field = input("Enter the field to update (FirstName, LastName, RollNumber, etc.): ")
new_value = input(f"Enter the new value for {field}: ")
# Delete student
def delete_student():
db = connect_to_database()
cursor = db.cursor()
student_id = input("Enter Student ID to delete: ")
query = "DELETE FROM Students WHERE StudentID = %s;"
cursor.execute(query, (student_id,))
db.commit()
print("Student deleted successfully.")
db.close()
# Add teacher
def add_teacher():
db = connect_to_database()
cursor = db.cursor()
query = """
INSERT INTO Teachers (FirstName, LastName, Subject, PhoneNumber, Email)
VALUES (%s, %s, %s, %s, %s);
"""
cursor.execute(query, (first_name, last_name, subject, phone, email))
db.commit()
print("Teacher added successfully.")
db.close()
# View teachers
def view_teachers():
db = connect_to_database()
cursor = db.cursor()
cursor.execute("SELECT * FROM Teachers;")
rows = cursor.fetchall()
print("\n--- Teacher Records ---")
for row in rows:
print(row)
db.close()
---
Instructions:
1. Replace database credentials (host, user, password, database) with your own.
This source code is a basic implementation. You can expand it to include additional features
such as attendance, fee management, or exam results. Let me know if you need further
customization!