Yehsahi
Yehsahi
py
1
2 def insert_student(connection, name, class_name, section, age, gender,
contact_number):
3 try:
4 cursor = connection.cursor()
5 query = "INSERT INTO Students (name, class, section, age,
gender, contact_number) VALUES (%s, %s, %s, %s, %s, %s)"
6 cursor.execute(query, (name, class_name, section, age, gender,
contact_number))
7 connection.commit()
8 print("Student inserted successfully.")
9 except mysql.connector.Error as err:
10 print(f"Error: {err}")
11 finally:
12 cursor.close()
13
14 def insert_student_flow(connection):
15 name = input("Enter student name: ")
16 class_name = input("Enter class: ")
17 section = input("Enter section: ")
18 age = int(input("Enter age: "))
19 gender = input("Enter gender (Male/Female): ")
20 contact_number = input("Enter contact number: ")
21 insert_student(connection, name, class_name, section, age, gender,
contact_number)
22
23
24 def insert_teacher(connection, name, subject, contact_number):
25 try:
26 cursor = connection.cursor()
27 query = "INSERT INTO Teachers (name, subject, contact_number)
VALUES (%s, %s, %s)"
28 cursor.execute(query, (name, subject, contact_number))
29 connection.commit()
30 print("Teacher inserted successfully.")
31 except mysql.connector.Error as err:
32 print(f"Error: {err}")
33 finally:
34 cursor.close()
35
36 def insert_teacher_flow(connection):
37 name = input("Enter teacher name: ")
38 subject = input("Enter subject: ")
39 contact_number = input("Enter contact number: ")
40 insert_teacher(connection, name, subject, contact_number)
41
42
43 def insert_class(connection, class_name, teacher_in_charge):
44 try:
45 cursor = connection.cursor()
46 query = "INSERT INTO Classes (class_name, teacher_in_charge)
VALUES (%s, %s)"
47 cursor.execute(query, (class_name, teacher_in_charge))
48 connection.commit()
49 print("Class inserted successfully.")
50 except mysql.connector.Error as err:
51 print(f"Error: {err}")
52 finally:
53 cursor.close()
54
55 def insert_class_flow(connection):
56 class_name = input("Enter class name: ")
57 teacher_id = int(input("Enter teacher in charge ID: "))
58 insert_class(connection, class_name, teacher_id)
59
60
61
62 def retrieve_students(connection):
63 cursor = connection.cursor()
64 cursor.execute("SELECT * FROM Students")
65 data = cursor.fetchall()
66 return data
67
68 def retrieve_students_fl
ow(connection):
69 try:
70 cursor = connection.cursor()
71 query = "SELECT * FROM Students"
72 cursor.execute(query)
73 # Fetch data and column description
74 students = cursor.fetchall()
75 description = cursor.description
76 # Pass both data and description to display data
77 display_data(students, description, "Students")
78 except Exception as e:
79 print(f"Error retrieving students data: {e}")
80 finally:
81 cursor.close()
82
83
84 def retrieve_teachers(connection):
85 cursor = connection.cursor()
86 cursor.execute("SELECT * FROM Teachers")
87 data = cursor.fetchall()
88 return data
89
90 def retrieve_teachers_fl
ow(connection):
91 try:
92 cursor = connection.cursor()
93 query = "SELECT * FROM Teachers"
94 cursor.execute(query)
95 # Fetch data and description
96 data = cursor.fetchall()
97 description = cursor.description
98 # Pass to display data
99 display_data(data, description, "Teachers")
100 except Exception as e:
101 print(f"Error retrieving teachers data: {e}")
102 finally:
103 cursor.close()
104
105
106 def retrieve_classes(connection):
107 cursor = connection.cursor()
108 cursor.execute("SELECT * FROM Classes")
109 data = cursor.fetchall()
110 return data
111
112 def retrieve_classes_flo
w(connection):
113 try:
114 cursor = connection.cursor()
115 query = "SELECT * FROM Classes"
116 cursor.execute(query)
117 # Fetch data and column description
118 data = cursor.fetchall()
119 description = cursor.description
120 # Pass both data and description to display data
121 display_data(data, description, "Classes")
122 except Exception as e:
123 print(f"Error retrieving classes data: {e}")
124 finally:
125 cursor.close()
126
127
128 def display_data(data, description, title):
129 if data:
130 # Extract column names from the cursor's description
131 columns = [col[0] for col in description]
132 # Create a DataFrame from the data and columns
133 df = pd.DataFrame(data, columns=columns)
134 print(f"\n--- {title} Data ---")
135 print(df.to_string(index=False))
136 else:
137 print(f"\nNo {title.lower()} data found")
138
139
140 def plot_avg_marks_per_s
ubject_per_class(connection):
141 """Plot a grouped bar graph of average marks per subject for each
class."""
142 try:
143 cursor = connection.cursor()
144 # Query to get class names, subjects, and their average marks
145 query = """
146 SELECT C.class_name, M.subject, AVG(M.marks) as
average_marks
147 FROM Classes C
148 JOIN Students S ON C.class_name = S.class
149 JOIN Marks M ON S.student_id = M.student_id
150 GROUP BY C.class_name, M.subject
151 ORDER BY C.class_name, M.subject
152 """
153 cursor.execute(query)
154 results = cursor.fetchall()
155 if results:
156 # Prepare data for plotting
157 data = defaultdict(lambda: {"subjects": [], "averages":
[]})
158 for row in results:
159 class_name, subject, avg_marks = row
160 data[class_name]["subjects"].append(subject)
161 data[class_name]["averages"].append(avg_marks)
162
163 # Assuming all classes have the same subjects
164 # Plot grouped bar chart
165 classes = list(data.keys())
166 subjects = data[classes[0]]["subjects"]
167 x = range(len(classes))
168 width = 0.2
169 plt.figure(figsize=(10, 6))
170 for i, subject in enumerate(subjects):
171 subject_averages = [data[class_name]["averages"][i]
for class_name in classes]
172 plt.bar([pos + i * width for pos in x],
subject_averages, width=width, label=subject)
173
174 # Customizing the plot
175 plt.xticks([pos + width for pos in x], classes)
176 plt.title("Average Marks Per Subject Per class",
fontsize=16)
177 plt.xlabel("Class Name", fontsize=12)
178 plt.ylabel("Average Marks", fontsize=12)
179 plt.legend(title="Subjects")
180 plt.grid(axis="y", linestyle="--", alpha=0.7)
181 plt.tight_layout()
182 plt.show()
183 else:
184 print("No data available to plot.")
185 except Exception as e:
186 print(f"Error plotting average marks per subject per class:
{e}")
187 finally:
188 cursor.close()
189