Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Source code for a basic School Management System in Python using MySQL

Uploaded by

selvaufo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Source code for a basic School Management System in Python using MySQL

Uploaded by

selvaufo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Here is the 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:

1. Install Python MySQL connector:

pip install mysql-connector-python

2. Set up the required MySQL tables as described earlier.

---

Python Source Code:

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()

first_name = input("Enter First Name: ")


last_name = input("Enter Last Name: ")
roll_number = input("Enter Roll Number: ")
class_id = input("Enter Class ID: ")
dob = input("Enter Date of Birth (YYYY-MM-DD): ")
gender = input("Enter Gender (Male/Female): ")
address = input("Enter Address: ")
phone = input("Enter Phone Number: ")
email = input("Enter Email: ")

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}: ")

query = f"UPDATE Students SET {field} = %s WHERE StudentID = %s;"


cursor.execute(query, (new_value, student_id))
db.commit()
print("Student updated successfully.")
db.close()

# 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()

first_name = input("Enter First Name: ")


last_name = input("Enter Last Name: ")
subject = input("Enter Subject: ")
phone = input("Enter Phone Number: ")
email = input("Enter Email: ")

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()

# Main program loop


def main():
while True:
choice = main_menu()
if choice == "1":
add_student()
elif choice == "2":
view_students()
elif choice == "3":
update_student()
elif choice == "4":
delete_student()
elif choice == "5":
add_teacher()
elif choice == "6":
view_teachers()
elif choice == "7":
print("Exiting the system. Goodbye!")
break
else:
print("Invalid choice. Please try again.")

# Run the program


if __name__ == "__main__":
main()

---

Instructions:

1. Replace database credentials (host, user, password, database) with your own.

2. Run the script in a Python environment.


3. Choose menu options to interact with the database.

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!

You might also like