CREATE DATABASE StudentDB;
USE StudentDB;
CREATE TABLE students (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Class VARCHAR(10),
Section CHAR(1)
);
CREATE TABLE fees (
Student_id INT,
Total_fee DECIMAL(10, 2),
Paid_fee DECIMAL(10, 2),
Due_fee DECIMAL(10, 2),
FOREIGN KEY (student_id) REFERENCES students(id)
);
CREATE TABLE guardians (
Student_id INT,
Guardian_name VARCHAR(100),
Contact_number VARCHAR(15),
Relation VARCHAR(50),
FOREIGN KEY (student_id) REFERENCES students(id)
);
CREATE TABLE library (
Book_id INT AUTO_INCREMENT PRIMARY KEY,
Book_title VARCHAR(200),
Issued_to_student_id INT,
Issue_date DATE,
Return_date DATE,
FOREIGN KEY (issued_to_student_id) REFERENCES students(id)
);
Import mysql.connector
# Connect to MySQL database
Def connect_to_db():
Return mysql.connector.connect(
Host=”localhost”, # Change this if using a different host
User=”root”, # Replace with your MySQL username
Password=”password”, # Replace with your MySQL password
Database=”StudentDB”
# Add a new student
Def add_student(name, age, student_class, section):
Db = connect_to_db()
Cursor = db.cursor()
Sql = “INSERT INTO students (name, age, class, section) VALUES (%s,
%s, %s, %s)”
Values = (name, age, student_class, section)
Cursor.execute(sql, values)
Db.commit()
Print(f”Student {name} added successfully!”)
Db.close()
# View all students
Def view_students():
Db = connect_to_db()
Cursor = db.cursor()
Cursor.execute(“SELECT * FROM students”)
Rows = cursor.fetchall()
Print(“ID | Name | Age | Class | Section”)
Print(“----------------------------------------“)
For row in rows:
Print(f”{row[0]} | {row[1]:<10} | {row[2]} | {row[3]} |
{row[4]}”)
Db.close()
# Search for a student by name
Def search_student(name):
Db = connect_to_db()
Cursor = db.cursor()
Sql = “SELECT * FROM students WHERE name LIKE %s”
Cursor.execute(sql, (f”%{name}%”,))
Rows = cursor.fetchall()
If rows:
Print(“ID | Name | Age | Class | Section”)
Print(“----------------------------------------“)
For row in rows:
Print(f”{row[0]} | {row[1]:<10} | {row[2]} | {row[3]} |
{row[4]}”)
Else:
Print(“No student found with that name.”)
Db.close()
# Delete a student by ID
Def delete_student(student_id):
Db = connect_to_db()
Cursor = db.cursor()
Sql = “DELETE FROM students WHERE id = %s”
Cursor.execute(sql, (student_id,))
Db.commit()
If cursor.rowcount > 0:
Print(f”Student with ID {student_id} deleted successfully!”)
Else:
Print(“No student found with that ID.”)
Db.close()
# Add or update fee details for a student
Def manage_fees(student_id, total_fee, paid_fee):
Db = connect_to_db()
Cursor = db.cursor()
# Calculate due fee
Due_fee = total_fee – paid_fee
# Check if the student already has a fee record
Cursor.execute(“SELECT * FROM fees WHERE student_id = %s”,
(student_id,))
If cursor.fetchone():
# Update existing record
Sql = “UPDATE fees SET total_fee = %s, paid_fee = %s, due_fee =
%s WHERE student_id = %s”
Cursor.execute(sql, (total_fee, paid_fee, due_fee, student_id))
Print(f”Fee record updated for Student ID {student_id}.”)
Else:
# Insert new record
Sql = “INSERT INTO fees (student_id, total_fee, paid_fee,
due_fee) VALUES (%s, %s, %s, %s)”
Cursor.execute(sql, (student_id, total_fee, paid_fee, due_fee))
Print(f”Fee record added for Student ID {student_id}.”)
Db.commit()
Db.close()
# View fee details for a student
Def view_fees(student_id):
Db = connect_to_db()
Cursor = db.cursor()
Sql = “SELECT * FROM fees WHERE student_id = %s”
Cursor.execute(sql, (student_id,))
Fee_details = cursor.fetchone()
If fee_details:
Print(f”Total Fee: {fee_details[1]}, Paid Fee: {fee_details[2]}, Due
Fee: {fee_details[3]}”)
Else:
Print(“No fee details found for this student.”)
Db.close()
# Add or update guardian information for a student
Def manage_guardian(student_id, guardian_name, contact_number,
relation):
Db = connect_to_db()
Cursor = db.cursor()
# Check if the student already has guardian details
Cursor.execute(“SELECT * FROM guardians WHERE student_id =
%s”, (student_id,))
If cursor.fetchone():
# Update existing record
Sql = “UPDATE guardians SET guardian_name = %s,
contact_number = %s, relation = %s WHERE student_id = %s”
Cursor.execute(sql, (guardian_name, contact_number, relation,
student_id))
Print(f”Guardian details updated for Student ID {student_id}.”)
Else:
# Insert new record
Sql = “INSERT INTO guardians (student_id, guardian_name,
contact_number, relation) VALUES (%s, %s, %s, %s)”
Cursor.execute(sql, (student_id, guardian_name,
contact_number, relation))
Print(f”Guardian details added for Student ID {student_id}.”)
Db.commit()
Db.close()
# View guardian information
Def view_guardian(student_id):
Db = connect_to_db()
Cursor = db.cursor()
Sql = “SELECT * FROM guardians WHERE student_id = %s”
Cursor.execute(sql, (student_id,))
Guardian_details = cursor.fetchone()
If guardian_details:
Print(f”Guardian Name: {guardian_details[1]}, Contact:
{guardian_details[2]}, Relation: {guardian_details[3]}”)
Else:
Print(“No guardian details found for this student.”)
Db.close()
# Search students based on filters
Def search_students_by_class(class_name, section=None):
Db = connect_to_db()
Cursor = db.cursor()
# Query with optional section filter
If section:
Sql = “SELECT * FROM students WHERE class = %s AND section =
%s”
Cursor.execute(sql, (class_name, section))
Else:
Sql = “SELECT * FROM students WHERE class = %s”
Cursor.execute(sql, (class_name,))
Rows = cursor.fetchall()
If rows:
Print(“ID | Name | Age | Class | Section”)
Print(“----------------------------------------“)
For row in rows:
Print(f”{row[0]} | {row[1]:<10} | {row[2]} | {row[3]} |
{row[4]}”)
Else:
Print(“No students found matching the criteria.”)
Db.close()
# Issue a book to a student
def issue_book(book_title, student_id, issue_date, return_date):
db = connect_to_db()
cursor = db.cursor()
sql = "INSERT INTO library (book_title, issued_to_student_id,
issue_date, return_date) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (book_title, student_id, issue_date,
return_date))
db.commit()
print(f"Book '{book_title}' issued to Student ID {student_id}.")
db.close()
# View issued books for a student
def view_issued_books(student_id):
db = connect_to_db()
cursor = db.cursor()
sql = "SELECT * FROM library WHERE issued_to_student_id = %s"
cursor.execute(sql, (student_id,))
books = cursor.fetchall()
if books:
print("Book ID | Book Title | Issue Date | Return Date")
print("---------------------------------------------------------")
for book in books:
print(f"{book[0]} | {book[1]:<20} | {book[3]} | {book[4]}")
else:
print("No books issued to this student.")
db.close()
# Main Menu
Def main():
While True:
Print(“\nStudent Management System”)
Print(“1. Add Student”)
Print(“2. View Students”)
Print(“3. Search Student”)
Print(“4. Delete Student”)
Print(“5.Fee Management System”)
Print(“6.Guardian Information”)
Print(“7.Search and Filtering Options”)
Print(“8.Library Management”)
Print(“9. Exit”)
Choice = input(“Enter your choice: “)
If choice == “1”:
Name = input(“Enter name: “)
Age = int(input(“Enter age: “))
Student_class = input(“Enter class: “)
Section = input(“Enter section: “)
Add_student(name, age, student_class, section)
Elif choice == “2”:
View_students()
Elif choice == “3”:
Name = input(“Enter name to search: “)
Search_student(name)
Elif choice == “4”:
Student_id = int(input(“Enter student ID to delete: “))
Delete_student(student_id)
Elif choice == “5”:
Student_id = int(input(“Enter Student ID: “))
Total_fee = float(input(“Enter Total Fee: “))
Paid_fee = float(input(“Enter Paid Fee: “))
Manage_fees(student_id, total_fee, paid_fee)
Elif choice == “6”:
Student_id = int(input(“Enter Student ID: “))
Guardian_name = input(“Enter Guardian Name: “)
Contact_number = input(“Enter Contact Number: “)
Relation = input(“Enter Relation: “)
Manage_guardian (student_id, guardian_name,
contact_number, relation)
Elif choice == “7”:
Class_name = input(“Enter Class: “)
Section = input(“Enter Section (leave blank if none): “).strip()
Search_students_by_class(class_name, section if section else
None)
Elif choice == “8”:
Student_id = int(input(“Enter Student ID: “))
Book_title = input(“Enter Book Title: “)
Issue_date = input(“Enter Issue Date (YYYY-MM-DD): “)
Return_date = input(“Enter Return Date (YYYY-MM-DD): “)
Issue_book(book_title, student_id, issue_date, return_date)
Elif choice == “9”:
Print(“Exiting the program.”)
Break
Else:
Print(“Invalid choice. Please try again.”)
If __name__ == “__main__”:
Main()