0% found this document useful (0 votes)
15 views2 pages

Student Project

python program

Uploaded by

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

Student Project

python program

Uploaded by

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

import mysql.

connector

# Establishing the connection to MySQL


conn = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="student_management1"
)

cursor = conn.cursor()

# Function to add a new student


def add_student():
name = input("Enter student's name: ")
roll_number = input("Enter student's roll number: ")
if name and roll_number:
try:
cursor.execute("INSERT INTO students (name, roll_number) VALUES (%s,
%s)", (name, roll_number))
conn.commit()
print("Student added successfully!")
except mysql.connector.Error as err:
print(f"Error: {err}")
conn.rollback()
else:
print("Please enter both name and roll number.")

# Function to view all students


def view_students():
cursor.execute("SELECT name, roll_number FROM students")
result = cursor.fetchall()
if not result:
print("No students found.")
else:
for student in result:
print(f"Name: {student[0]}, Roll Number: {student[1]}")

# Function to delete a student


def delete_student():
roll_number = input("Enter the roll number of the student to delete: ")
if roll_number:
cursor.execute("DELETE FROM students WHERE roll_number = %s",
(roll_number,))
if cursor.rowcount > 0:
conn.commit()
print("Student deleted successfully!")
else:
print("Student not found!")
conn.rollback()
else:
print("Please enter a roll number.")

# Command-line interface for the system


def main():
while True:
print("\nStudent Management System")
print("1. Add Student")
print("2. View Students")
print("3. Delete Student")
print("4. Exit")

choice = input("Choose an option: ")

if choice == '1':
add_student()
elif choice == '2':
view_students()
elif choice == '3':
delete_student()
elif choice == '4':
break
else:
print("Invalid option, please try again.")

# Run the application


if __name__ == "__main__":
main()

# Close the database connection


conn.close()

You might also like