Project File
Python with MySql connectivity
Presented by:
Krish Chaudhary
1
UNDERTAKING
I, Krish Chaudhary, solemnly undertake
that the project titled "Assignment
Manager," which is based on Python and
MySQL integration, has been developed by
me. I assure that this project is my original
work and that it does not infringe upon the
intellectual property rights of any other
individual or organization. I take full
responsibility for the content and
functionality of this project.
2
CERTIFICATE
This is to certify that the project titled
"Assignment
Manager" by Krish Chaudhary has been
successfully completed.
The project demonstrates proficiency in Python
and MySQL integration for managing student's
assignments.
It includes features for adding students,
assigning and recording assignments,
calculating work completion, and displaying
relevant information. This project has met the
requirements and objectives set forth.
3
ACKNOWLEDEMENT
I would like to express my sincere gratitude
to Ms. lakshmi, my Computer PGT, for her
guidance and support throughout the
development of this project.
Her valuable insights and assistance have
been instrumental in the successful
completion of the "Assignment Manager"
project.
I would also like to acknowledge the support
of my friend Harsh Chaudhary , peers and the
resources provided by my educational
institution, which have contributed to the
completion of this project.
It was a great learning experience
4
SOURCE CODE:
import [Link]
# Connect to the MySQL server
#change this according to your preference
db = [Link](
host="localhost",
user="root",
password="123456",
database="assignment_manager"
)
# Create a cursor object to interact with the database
cursor = [Link]()
# Create the tables if they don't exist
[Link]("""""
CREATE TABLE IF NOT EXISTS students (
id INT AUTO_INCREMENT PRIMARY KEY,
roll_no INT UNIQUE,
name VARCHAR(255)
)
"***")
5
[Link]("""""
CREATE TABLE IF NOT EXISTS assignments (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
subject VARCHAR(255),
chapter VARCHAR(255),
assignment_no INT,
assignment_type VARCHAR(255),
marks_total INT,
marks_gained INT,
FOREIGN KEY (student_id) REFERENCES students(id)
)
""""")
#Function to add a student to the database
def add_student(roll_no, name):
try:
query = "INSERT INTO students (roll_no, name) VALUES (%s,
%s)"
values = (roll_no, name)
[Link](query, values)
[Link]()
print("Student added successfully!")
except [Link] as error:
print("Failed to add student:", error)
6
# Function to add an assignment for a student
def add_assignment(student_id, subject, chapter, assignment_no,
assignment_type, marks_total, marks_gained):
try:
query = "INSERT INTO assignments (student_id, subject, chapter,
assignment_no, assignment_type, marks_total, marks_gained) VALUES (%s,
%s, %s, %s, %s, %s, %s)"
values = (student_id, subject, chapter, assignment_no, assignment_type,
marks_total, marks_gained)
[Link](query, values)
[Link]()
print("Assignment added successfully!")
except [Link] as error:
print("Failed to add assignment:", error)
# Function to calculate the average work completion for a student
def calculate_average_completion(student_id):
try:
query = "SELECT AVG(marks_gained / marks_total) FROM assignments
WHERE student_id=%s"
values = (student_id,)
[Link](query, values)
result = [Link]()[0]
return result
except [Link] as error:
print("Failed to calculate average completion:", error)
7
return None
#Function to display the students table
def display_students():
try:
[Link]("SELECT * FROM students")
students [Link]()
if students:
print("Students Table:")
print("------------")
print("ID\tRoll No\tName")
for student in students:
print(f"{student[0]}\t{student[1]}\t{student[2]}")
else:
print("No students found.")
except [Link] as error:
print("Failed to display students:", error)
# Function to display the assignments table
def display_assignments():
try:
[Link]("SELECT * FROM assignments")
assignments = [Link]()
8
if assignments:
print("Assignments Table:")
print("--------------")
print("ID\tStudentID\tSubject\tChapter\tAssignmen No\tAssignment
TypeltMarks Total\tMarks Gained")
for assignment in assignments:
print(f" {assignment[0]}\t{assignment[1]}\t\t{assignment[2]}\t{assign
ment[3]}\t\t{assignment[4]}\t\t{assignment[5]}\t\t{assignment[6]}\t\t{a
ssignment[7]}")
else:
print("No assignments found.")
except [Link] as error:
print("Failed to display assignments:", error)
#Function to display details of a specific student from both tables
def display_student_details(student_id):
try:
query = "SELECT * FROM students WHERE id = %s"
values = (student_id,)
[Link](query, values)
student [Link]()
if student:
print("Student Details:")
print("----------------")
print(f"ID: {student[0]}")
9
print("Roll No: {student[1]}")
print(f"Name: {student[2]}")
query= "SELECT * FROM assignments WHERE student_id =
%S"
[Link](query, values)
assignments = [Link]()
if assignments:
print("\nAssignments:")
print("------------")
print("ID\tSubject\tChapter\tAssignment No\tAssignment
Type\tMarks Total\tMarks Gained")
for assignment in assignments:
print("{assignment[0]}\t{assignment[2]}\t{assignment[3]}\t\t{assign
ment[4]}\t\t{assignment[5]}\t\t{assignment[6]} \t\t{assignment[7]}")
else:
print("No assignments found for this student.")
else:
print("Student not found.")
except [Link] as error:
print("Failed to display student details:", error)”
# Function to display a list of students who have completed an
assignment of a particular chapter
def display_students_completed_assignment(chapter):
10
try:
query = "SELECT [Link], [Link],
assignments.marks_gained FROM students INNER JOIN assignments ON
[Link]= assignments.student_id WHERE
[Link]=%s"
values = (chapter,)
[Link](query, values)
students = [Link]()
if students:
print(f"Students who have completed the assignment for
Chapter '{chapter}":")
print("ID\tName\tMarks Gained")
for student in students:
print(f"{student[0]}\t{student[1]}\t{student[2]}")
else:
print("No students found who have completed the assignment
for this chapter.")
except [Link] as error:
print("Failed to display students who completed the
assignment:", error)
#Teacher interface to save data in tables and view information
def teacher_interface():
print("Welcome, Teacher!")
print("-------------------")
11
while True:
print("\nMenu:")
print("1. Add Student")
print("2. Add Assignment")
print("3. View Students Table")
print("4. View Assignments Table")
print("5. Display Student Details")
print("6. Display Students Completed Assignment")
print("7. Calculate Average Work Completion") print("8. Exit")
choice = input("Enter your choice (1-8): ")
if choice == "1":
roll_no = int(input("Enter Roll No: "))
name = input("Enter Name: ")
add_student(roll_no, name)
elif choice == "2":
student_id = int(input("Enter Student ID: "))
subject = input("Enter Subject: ")
chapter = input("Enter Chapter: ")
assignment_no = int(input("Enter Assignment No: "))
assignment_type = input("Enter Assignment Type: ")
marks_total = int(input("Enter Total Marks: "))
marks_gained = int(input("Enter Marks Gained: "))
12
add_assignment(student_id, subject, chapter, assignment_no,
assignment_type, marks_total, marks_gained)
elif choice == "3":
display_students()
elif choice == "4":
display_assignments()
elif choice == "5":
student_id = int(input("Enter Student ID: "))
display_student_details(student_id)
elif choice == "6":
chapter = input("Enter Chapter: ")
display_students_completed_assignment(chapter)
elif choice == "7":
student_id = int(input("Enter Student ID: "))
average_completion = calculate_average_completion(student_id)
if average_completion is not None:
print(f"Average work completion:{average_completion:.2%}")
elif choice == "8":
break
else:
print("Invalid choice! Please try again.")
print("\nGoodbye, Teacher!")
13
# Run the teacher interface
teacher_interface()
# Close the database connection
[Link]()
#NOTE: CHANGE THE USERNAME AND #PASSWORD
TO YOUR USERNAME AND #PASSWORD FOR
TESTING OF CODE
14
SAMPLE MYSQL
cOMMANDS FOR TESTING
15
• Adding a student:
• Adding a assignment:
• Viewing students table:
16
• Viewing assignments table:
• Displaying student details:
• Displaying students who completed an
assignment for a specific chapter:
• Calculating average work completion for a
student:
17