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

NISHI

This Python script connects to a MySQL database to manage student attendance. It includes functions to add students, mark attendance, view attendance records, and calculate monthly attendance percentages. The script runs a menu-driven interface allowing users to perform these actions interactively.

Uploaded by

skvjict
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)
3 views7 pages

NISHI

This Python script connects to a MySQL database to manage student attendance. It includes functions to add students, mark attendance, view attendance records, and calculate monthly attendance percentages. The script runs a menu-driven interface allowing users to perform these actions interactively.

Uploaded by

skvjict
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

SOURCE CODE:

import mysql.connector

from datetime import date

# Connect to MySQL

conn = mysql.connector.connect(

host="localhost",

user="root", # Replace with your MySQL username

password="123456", # Replace with your MySQL password

database="attendance_db" # Replace with your database name

cursor = conn.cursor()

# Function to add a student

def add_student(name, course):

query = "INSERT INTO students (name, course) VALUES (%s, %s)"

#print(f"Executing SQL: {query} with values {name}, {course}")

cursor.execute(query, (name, course))

conn.commit()

print("Student added successfully.")

# Function to mark attendance

def mark_attendance(student_id, status):

today = date.today()

query = "INSERT INTO attendance (student_id, date, status) VALUES (%s, %s, %s)"

# print(f"Executing SQL: {query} with values {student_id}, {today}, {status}")

cursor.execute(query, (student_id, today, status))

conn.commit()

print("Attendance marked.")
# Function to view attendance

def view_attendance():

query = """

SELECT s.student_id, s.name, s.course, a.date, a.status

FROM students s

JOIN attendance a ON s.student_id = a.student_id

ORDER BY a.date DESC

"""

#print(f"Executing SQL: {query}")

cursor.execute(query)

records = cursor.fetchall()

print("\n--- Attendance Records ---")

for row in records:

print(f"ID: {row[0]}, Name: {row[1]}, Course: {row[2]}, Date: {row[3]}, Status: {row[4]}")

# Function to calculate monthly attendance

def calculate_monthly_attendance(student_id, month, year):

query = """

SELECT COUNT(*)

FROM attendance

WHERE student_id = %s AND status = 'Present'

AND MONTH(date) = %s AND YEAR(date) = %s

"""

# print(f"Executing SQL: {query} with values {student_id}, {month}, {year}")

cursor.execute(query, (student_id, month, year))

present_days = cursor.fetchone()[0]

# Get total working days in the month


query_total = """

SELECT COUNT(*)

FROM attendance

WHERE student_id = %s AND MONTH(date) = %s AND YEAR(date) = %s

"""

# print(f"Executing SQL: {query_total} with values {student_id}, {month}, {year}")

cursor.execute(query_total, (student_id, month, year))

total_days = cursor.fetchone()[0]

print(f"\nMonthly Attendance for Student ID {student_id}:")

print(f"Present Days: {present_days}")

print(f"Total Days: {total_days}")

attendance_percentage = (present_days / total_days) * 100 if total_days > 0 else 0

print(f"Attendance Percentage: {attendance_percentage:.2f}%\n")

# Main menu

while True:

print("\n--- Attendance Management System ---")

print("1. Add Student")

print("2. Mark Attendance")

print("3. View Attendance")

print("4. Calculate Monthly Attendance")

print("5. Exit")

choice = input("Enter your choice: ")

if choice == '1':

name = input("Enter student name: ")

course = input("Enter course: ")

add_student(name, course)
elif choice == '2':

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

status = input("Enter status (Present/Absent): ")

mark_attendance(student_id, status)

elif choice == '3':

view_attendance()

elif choice == '4':

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

month = int(input("Enter month (1-12): "))

year = int(input("Enter year (e.g., 2025): "))

calculate_monthly_attendance(student_id, month, year)

elif choice == '5':

print("Exiting...")

break

else:

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

# Close connection

cursor.close()

conn.close()
OUTPUT:

You might also like