0% found this document useful (0 votes)
11 views7 pages

Untitled Document

This document is a Python script that connects to a MySQL database for managing student records. It includes functionalities for creating a database and tables, adding and updating student information, entering marks, and generating report cards. The script provides a main menu for user interaction to perform various operations related to student management.

Uploaded by

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

Untitled Document

This document is a Python script that connects to a MySQL database for managing student records. It includes functionalities for creating a database and tables, adding and updating student information, entering marks, and generating report cards. The script provides a main menu for user interaction to perform various operations related to student management.

Uploaded by

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

import os

import mysql.connector as ms
from datetime import datetime

# Connect to the MySQL server with error handling


try:
mydb = ms.connect(host="localhost", user="root",
password="Chomieeee", port=3307)
mycursor = mydb.cursor()
except ms.Error as err:
print(f"Error: {err}")
exit()

# Create the database if it doesn't exist


def create_database():
try:
mycursor.execute("CREATE DATABASE IF NOT EXISTS
student_management")
mycursor.execute("USE student_management")
except ms.Error as err:
print(f"Error while creating or using the database: {err}")
exit()

# Create tables if not exists


def create_tables():
try:
# Creating STUDENT table
create_student_table = """
CREATE TABLE IF NOT EXISTS STUDENT (
SROLL_NO VARCHAR(5),
SNAME VARCHAR(30),
FNAME VARCHAR(30),
MNAME VARCHAR(30),
PHONE VARCHAR(12),
ADDRESS VARCHAR(100),
SCLASS VARCHAR(5),
SSECTION VARCHAR(5),
SADMISSION_NO VARCHAR(10) PRIMARY KEY,
CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
mycursor.execute(create_student_table)
# Creating MARKS table
create_marks_table = """
CREATE TABLE IF NOT EXISTS MARKS (
SADMISSION_NO VARCHAR(10) PRIMARY KEY,
HINDI INT,
ENGLISH INT,
MATH INT,
SCIENCE INT,
SOCIAL INT,
COMPUTER INT,
TOTAL INT,
AVERAGE DECIMAL(5,2),
CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
mycursor.execute(create_marks_table)
except ms.Error as err:
print(f"Error: {err}")
exit()

# Validate integer input


def validate_integer_input(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Invalid input. Please enter a number.")
# MODULE FOR NEW ADMISSION
def new_student():
print("Enter Student Details:")
sroll_no = input("Enter Roll No: ")
sname = input("Enter Student's Name: ")
fname = input("Enter Father's Name: ")
mname = input("Enter Mother's Name: ")
phone = input("Enter Contact No. (10 digits): ")
address = input("Enter Address: ")
sclass = input("Enter Class: ")
ssection = input("Enter Section: ")
sadmission_no = input("Enter Admission No: ")

confirmation = input(f"Are you sure you want to add student


{sname}? (yes/no): ")
if confirmation.lower() == "yes":
sql = """
INSERT INTO STUDENT (SROLL_NO, SNAME, FNAME, MNAME, PHONE,
ADDRESS, SCLASS, SSECTION, SADMISSION_NO)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
values = (sroll_no, sname, fname, mname, phone, address,
sclass, ssection, sadmission_no)
try:
mycursor.execute(sql, values)
mydb.commit()
print(f"{sname}'s data added successfully!")
except ms.Error as err:
print(f"Error: {err}")
else:
print("Action canceled.")

# MODULE TO DISPLAY STUDENT'S DATA


def display_students():
mycursor.execute("SELECT * FROM STUDENT")
data = mycursor.fetchall()
if data:
for row in data:
print(row)
else:
print("No data found.")
# MODULE TO UPDATE STUDENT'S RECORD
def update_student():
admission_no = input("Enter Admission No: ")
sql = "SELECT * FROM STUDENT WHERE SADMISSION_NO = %s"
mycursor.execute(sql, (admission_no,))
data = mycursor.fetchall()
if data:
print("Press 1 for Name")
print("Press 2 for Class")
print("Press 3 for Roll No")
choice = validate_integer_input("Enter Your Choice: ")
if choice == 1:
name = input("Enter new name: ")
sql = "UPDATE STUDENT SET SNAME = %s WHERE SADMISSION_NO
= %s"
mycursor.execute(sql, (name, admission_no))
elif choice == 2:
std_class = input("Enter new class: ")
sql = "UPDATE STUDENT SET SCLASS = %s WHERE
SADMISSION_NO = %s"
mycursor.execute(sql, (std_class, admission_no))
elif choice == 3:
roll_no = input("Enter new roll no: ")
sql = "UPDATE STUDENT SET SROLL_NO = %s WHERE
SADMISSION_NO=%s"
mycursor.execute(sql, (roll_no, admission_no))
else:
print("Invalid choice!")
return
mydb.commit()
print("Record updated.")
else:
print("Record not found.")
# MODULE TO ENTER MARKS OF THE STUDENT
def marks_student():
print("Enter Marks Details:")
admission_no = input("Enter Admission No: ")
hindi = validate_integer_input("Enter Marks for Hindi: ")
english = validate_integer_input("Enter Marks for English: ")
math = validate_integer_input("Enter Marks for Math: ")
science = validate_integer_input("Enter Marks for Science: ")
social = validate_integer_input("Enter Marks for Social: ")
computer = validate_integer_input("Enter Marks for Computer: ")
total = hindi + english + math + science + social + computer
average = total / 6

confirmation = input(f"Are you sure you want to add marks for


Admission No {admission_no}? (yes/no): ")
if confirmation.lower() == "yes":
sql = """
INSERT INTO MARKS (SADMISSION_NO, HINDI, ENGLISH, MATH,
SCIENCE, SOCIAL, COMPUTER, TOTAL, AVERAGE)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
values = (admission_no, hindi, english, math, science,
social, computer, total, average)
try:
mycursor.execute(sql, values)
mydb.commit()
print("Marks entered successfully!")
except ms.Error as err:
print(f"Error: {err}")
else:
print("Action canceled.")
# MODULE TO FETCH REPORT CARD
def fetch_report_card(admission_no=None):
if admission_no:
sql = "SELECT * FROM MARKS WHERE SADMISSION_NO = %s"
mycursor.execute(sql, (admission_no,))
else:
sql = "SELECT * FROM MARKS"
mycursor.execute(sql)
return mycursor.fetchall()

# MODULE TO GENERATE REPORT CARD


def report_card(admission_no=None):
data = fetch_report_card(admission_no)
if data:
for row in data:
print(row)
else:
print("No data found.")

# MODULE TO EXIT
def exit_program():
exit_choice = input("Are you sure you want to exit? (yes/no): ")
if exit_choice.lower() == "yes":
mycursor.close()
mydb.close()
quit()
else:
print("Continuing with the menu...")

# MODULE TO DISPLAY HELP


def help_me():
print("Please visit the official website of Vidyalaya to
download the manual!")

# Initialize database and tables


create_database()
create_tables()
# Main Menu
def main_menu():
while True:
print("\nEnter 1 - Add Student")
print("Enter 2 - Display Student's Data")
print("Enter 3 - Update Student's Data")
print("Enter 4 - Add Student's Marks Detail")
print("Enter 5 - Generate All Students' Report Card")
print("Enter 6 - Generate Student-Wise Report Card")
print("Enter 7 - Exit")
print("Enter 0 - Help")
choice = validate_integer_input("Please Enter Your Choice:
")

if choice == 1:
new_student()
elif choice == 2:
display_students()
elif choice == 3:
update_student()
elif choice == 4:
marks_student()
elif choice == 5:
report_card()
elif choice == 6:
report_card(input("Enter Admission No: "))
elif choice == 7:
exit_program()
elif choice == 0:
help_me()
else:
print("Invalid input. Please try again.")

# Start the menu


main_menu()

You might also like