0% found this document useful (0 votes)
18 views4 pages

SOURCE CODE (cs)

The document contains a Python script for a Student Management System that connects to a MySQL database. It includes functions to add, delete, update, display, and search for student records. The program runs in a loop, allowing users to perform various operations until they choose to exit.

Uploaded by

kaurishmeen12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

SOURCE CODE (cs)

The document contains a Python script for a Student Management System that connects to a MySQL database. It includes functions to add, delete, update, display, and search for student records. The program runs in a loop, allowing users to perform various operations until they choose to exit.

Uploaded by

kaurishmeen12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SOURCE CODE

import mysql.connector

# Connect to MySQL database

conn=mysql.connector.connect(host="localhost",user="root”, password="a963*",
database="school")

cursor = conn.cursor()

# Function to add a student

def add_student():

student_id=int(input("enter student id: "))

name=input("Enter student name: ")

age = int(input("Enter student age: "))

marks = float(input("Enter student marks: "))

query = "INSERT INTO students (student_id,name, age, marks) VALUES (%s, %s, %s, %s)"

print('student added succesfully..!!!')

cursor.execute(query, (student_id, name, age, marks))

conn.commit()

# Function to delete a student

def delete_student():

student_id = int(input("Enter student ID to delete: "))

query = "DELETE FROM students WHERE student_id =%s"

cursor.execute(query, (student_id,))
conn.commit()

print("Student deleted successfully!")

# Function to update a student

def update_student():

student_id = int(input("Enter student ID to update: "))

name = input("Enter new name: ")

age = int(input("Enter new age: "))

marks = float(input("Enter new marks: "))

query = "UPDATE students SET name = %s, age = %s,marks = %s WHERE student_id = %s"

cursor.execute(query, (name, age, marks, student_id))

conn.commit()

print("Student updated successfully!")

# Function to display all students

def display_students():

query = "SELECT * FROM students"

cursor.execute(query)

results = cursor.fetchall()

print("\nStudent Records:")

for row in results:

print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Marks: {row[3]}")

print()
# Function to search for a student

def search_student():

student_id = int(input("Enter student ID to search: "))

query = "SELECT * FROM students WHERE student_id = %s"

cursor.execute(query, (student_id,))

result = cursor.fetchone()

if result:

print(f"ID: {result[0]}, Name: {result[1]}, Age: {result[2]}, Marks: {result[3]}")

else:

print("Student not found!")

# Main program

print("""

“*****************************************************************************
*************************STUDENT MANAGEMENT SYSTEM ********************
******************************************************************************
******************************************************

""")

while True:

print("1. Add Student")

print("2. Delete Student")

print("3. Update Student")

print("4. Display All Students")

print("5. Search for a Student")

print("6. Exit")
choice = int(input("Enter your choice: "))

if choice == 1:

add_student()

elif choice == 2:

delete_student()

elif choice == 3:

update_student()

elif choice == 4:

display_students()

elif choice == 5:

search_student()

elif choice == 6:

print("Exiting the program...")

break

else:

print("Invalid choice! Please try again.")

# Close the connection

cursor.close()

conn.close()

You might also like