import mysql.
connector
# Connect to the MySQL Database
def connect_db():
return mysql.connector.connect(
host="localhost", # Replace with your MySQL host
user="root", # Replace with your MySQL username
password="", # Replace with your MySQL password
database="student_management"
# Add a new student
def add_student():
name = input("Enter student name: ")
student_class = input("Enter student class: ")
rollno = input("Enter student roll number: ")
section = input("Enter student section: ")
print("Enter marks for the following subjects (out of 100):")
physics = int(input("Physics: "))
chemistry = int(input("Chemistry: "))
maths = int(input("Mathematics: "))
cs = int(input("Computer Science: "))
english = int(input("English: "))
connection = connect_db()
cursor = connection.cursor()
query = """INSERT INTO students (name, class, rollno, section, physics, chemistry, maths, cs, english)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
cursor.execute(query, (name, student_class, rollno, section, physics, chemistry, maths, cs, english))
connection.commit()
print("Student added successfully!")
cursor.close()
connection.close()
# View all students
def view_students():
connection = connect_db()
cursor = connection.cursor()
cursor.execute("SELECT * FROM students")
students = cursor.fetchall()
if students:
print("\nID | Name | Class | Roll No | Section | Physics | Chemistry | Maths | CS | English")
for student in students:
print(f"{student[0]} | {student[1]} | {student[2]} | {student[3]} | {student[4]} | {student[5]} |
{student[6]} | {student[7]} | {student[8]} | {student[9]}")
else:
print("No students found.")
cursor.close()
connection.close()
# Delete a student by ID
def delete_student():
student_id = int(input("Enter student ID to delete: "))
connection = connect_db()
cursor = connection.cursor()
query = "DELETE FROM students WHERE id = %s"
cursor.execute(query, (student_id,))
connection.commit()
if cursor.rowcount > 0:
print("Student deleted successfully!")
else:
print("No student found with that ID.")
cursor.close()
connection.close()
# Update student details by ID
def update_student():
student_id = int(input("Enter student ID to update: "))
# Get the new details for the student
name = input("Enter new student name: ")
student_class = input("Enter new student class: ")
rollno = input("Enter new student roll number: ")
section = input("Enter new student section: ")
print("Enter new marks for the following subjects (out of 100):")
physics = int(input("Physics: "))
chemistry = int(input("Chemistry: "))
maths = int(input("Mathematics: "))
cs = int(input("Computer Science: "))
english = int(input("English: "))
connection = connect_db()
cursor = connection.cursor()
# Update the student record in the database
query = """UPDATE students
SET name = %s, class = %s, rollno = %s, section = %s, physics = %s, chemistry = %s, maths = %s,
cs = %s, english = %s
WHERE id = %s"""
cursor.execute(query, (name, student_class, rollno, section, physics, chemistry, maths, cs, english,
student_id))
connection.commit()
if cursor.rowcount > 0:
print("Student updated successfully!")
else:
print("No student found with that ID.")
cursor.close()
connection.close()
# Main menu to interact with the system
def main():
while True:
print("\nStudent Management System")
print("1. Add Student")
print("2. View Students")
print("3. Delete Student")
print("4. Update Student")
print("5. Exit")
choice = input("Enter your choice (1/2/3/4/5): ")
if choice == '1':
add_student()
elif choice == '2':
view_students()
elif choice == '3':
delete_student()
elif choice == '4':
update_student()
elif choice == '5':
print("Exiting the system.")
break
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
main()