0% found this document useful (0 votes)
31 views10 pages

Class 12 Ip Radha

This document provides a guide on setting up a student database management system using Python, MySQL, and Pandas. It includes prerequisites, steps to create a MySQL database and table, Python code for connecting to the database, and functionalities for adding, updating, and deleting student records. Additionally, it discusses limitations and future enhancements for the system.

Uploaded by

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

Class 12 Ip Radha

This document provides a guide on setting up a student database management system using Python, MySQL, and Pandas. It includes prerequisites, steps to create a MySQL database and table, Python code for connecting to the database, and functionalities for adding, updating, and deleting student records. Additionally, it discusses limitations and future enhancements for the system.

Uploaded by

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

Prerequisites:

1. Python installed (preferably Python 3).

2. MySQL installed and running.

3. Install Pandas and MySQL Connector using the following command:

pip install pandas mysql-connector-python

---

1. Set up MySQL Database:

First, you need to set up a MySQL database and a table to store the student
records.

Steps to set up:

1. Log in to your MySQL server:

mysql -u root -p

2. Create a new database:

CREATE DATABASE student_db;


USE student_db;

3. Create a students table with fields like RollNo, Name, Class, Marks:

CREATE TABLE students (


RollNo INT PRIMARY KEY,
Name VARCHAR(100),
Class VARCHAR(50),
Marks FLOAT
);

---

2. Python Code to Connect Pandas with MySQL:

Now, we’ll write a Python script to connect to MySQL, retrieve student records, and
allow for various operations like adding, updating, and deleting students.

import mysql.connector
import pandas as pd

# Connect to MySQL
def connect_db():
return mysql.connector.connect(
host="localhost", # Change this to your MySQL host if it's not localhost
user="root", # Change this to your MySQL username
password="password", # Change this to your MySQL password
database="student_db"
)

# Fetch all students from the MySQL database into a Pandas DataFrame
def fetch_students():
db = connect_db()
query = "SELECT * FROM students"
df = pd.read_sql(query, con=db)
db.close()
return df

# Add a new student


def add_student(roll_no, name, student_class, marks):
db = connect_db()
cursor = db.cursor()
query = "INSERT INTO students (RollNo, Name, Class, Marks) VALUES (%s, %s, %s,
%s)"
cursor.execute(query, (roll_no, name, student_class, marks))
db.commit()
db.close()

# Update student marks


def update_marks(roll_no, marks):
db = connect_db()
cursor = db.cursor()
query = "UPDATE students SET Marks = %s WHERE RollNo = %s"
cursor.execute(query, (marks, roll_no))
db.commit()
db.close()

# Delete a student
def delete_student(roll_no):
db = connect_db()
cursor = db.cursor()
query = "DELETE FROM students WHERE RollNo = %s"
cursor.execute(query, (roll_no,))
db.commit()
db.close()

# Main program flow


def main():
print("Welcome to the Student Database System")

while True:
print("\n1. View all students")
print("2. Add a new student")
print("3. Update student marks")
print("4. Delete a student")
print("5. Exit")

choice = input("Enter your choice: ")

if choice == "1":
df = fetch_students()
print("\nStudent Records:")
print(df)

elif choice == "2":


roll_no = int(input("Enter Roll No: "))
name = input("Enter Name: ")
student_class = input("Enter Class: ")
marks = float(input("Enter Marks: "))
add_student(roll_no, name, student_class, marks)
print("Student added successfully!")

elif choice == "3":


roll_no = int(input("Enter Roll No to update marks: "))
marks = float(input("Enter new marks: "))
update_marks(roll_no, marks)
print("Marks updated successfully!")

elif choice == "4":


roll_no = int(input("Enter Roll No to delete student: "))
delete_student(roll_no)
print("Student deleted successfully!")

elif choice == "5":


print("Exiting the system...")
break
else:
print("Invalid choice, please try again.")

if _name_ == "_main_":
main()

---

3. How the Program Works:

1. Connection to MySQL Database:

The connect_db() function connects to the MySQL server using the credentials
provided.

2. Fetching Data:

The fetch_students() function retrieves student records from the database and loads
them into a Pandas DataFrame.

The DataFrame allows for easy viewing and manipulation of the data.

3. Add, Update, Delete Operations:

Add student: Insert a new record into the students table.

Update marks: Update the marks for an existing student by Roll No.

Delete student: Delete a student record based on Roll No.


4. Main Menu System:

The user can choose between various options like viewing all students, adding a
student, updating marks, deleting a student, or exiting the program.

---

4. Sample Output:

Welcome to the Student Database System

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 1

Student Records:
RollNo Name Class Marks
0 101 Alice 12A 85.0
1 102 Bob 12B 72.0
2 103 Charlie 12A 91.0

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 2
Enter Roll No: 104
Enter Name: David
Enter Class: 12B
Enter Marks: 78
Student added successfully!

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 3
Enter Roll No to update marks: 102
Enter new marks: 80
Marks updated successfully!

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 4
Enter Roll No to delete student: 101
Student deleted successfully!

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 5
Exiting the system...

---

5. Limitations and Future Enhancements:

Limitations:

Single-threaded: The system is not designed for concurrent users.

No advanced features: The program lacks advanced functionalities such as attendance


tracking, class performance analysis, or reporting.

Basic validation: No input validation to handle incorrect inputs (e.g., entering a


string when numeric values are expected).

Future Enhancements:

GUI: A graphical user interface (e.g., using Tkinter) for a more user-friendly
experience.

Cloud Database: Storing the data on a cloud platform like Firebase for remote
access.

Advanced Reporting: Adding features to generate charts/graphs using libraries like


Matplotlib.

Security: Implementing user authentication to prevent unauthorized access.

---

Conclusion:

This project demonstrates how to connect Pandas with MySQL to create a simple yet
powerful student database management system. The use of Pandas allows for easy data
manipulation and analysis, while MySQL serves as a reliable backend for storing
student records. You can easily extend this project with additional features to
make it a comprehensive tool for school management.Prerequisites:

1. Python installed (preferably Python 3).

2. MySQL installed and running.

3. Install Pandas and MySQL Connector using the following command:


pip install pandas mysql-connector-python

---

1. Set up MySQL Database:

First, you need to set up a MySQL database and a table to store the student
records.

Steps to set up:

1. Log in to your MySQL server:

mysql -u root -p

2. Create a new database:

CREATE DATABASE student_db;


USE student_db;

3. Create a students table with fields like RollNo, Name, Class, Marks:

CREATE TABLE students (


RollNo INT PRIMARY KEY,
Name VARCHAR(100),
Class VARCHAR(50),
Marks FLOAT
);

---

2. Python Code to Connect Pandas with MySQL:

Now, we’ll write a Python script to connect to MySQL, retrieve student records, and
allow for various operations like adding, updating, and deleting students.

import mysql.connector
import pandas as pd

# Connect to MySQL
def connect_db():
return mysql.connector.connect(
host="localhost", # Change this to your MySQL host if it's not localhost
user="root", # Change this to your MySQL username
password="password", # Change this to your MySQL password
database="student_db"
)

# Fetch all students from the MySQL database into a Pandas DataFrame
def fetch_students():
db = connect_db()
query = "SELECT * FROM students"
df = pd.read_sql(query, con=db)
db.close()
return df

# Add a new student


def add_student(roll_no, name, student_class, marks):
db = connect_db()
cursor = db.cursor()
query = "INSERT INTO students (RollNo, Name, Class, Marks) VALUES (%s, %s, %s,
%s)"
cursor.execute(query, (roll_no, name, student_class, marks))
db.commit()
db.close()

# Update student marks


def update_marks(roll_no, marks):
db = connect_db()
cursor = db.cursor()
query = "UPDATE students SET Marks = %s WHERE RollNo = %s"
cursor.execute(query, (marks, roll_no))
db.commit()
db.close()

# Delete a student
def delete_student(roll_no):
db = connect_db()
cursor = db.cursor()
query = "DELETE FROM students WHERE RollNo = %s"
cursor.execute(query, (roll_no,))
db.commit()
db.close()

# Main program flow


def main():
print("Welcome to the Student Database System")

while True:
print("\n1. View all students")
print("2. Add a new student")
print("3. Update student marks")
print("4. Delete a student")
print("5. Exit")

choice = input("Enter your choice: ")

if choice == "1":
df = fetch_students()
print("\nStudent Records:")
print(df)

elif choice == "2":


roll_no = int(input("Enter Roll No: "))
name = input("Enter Name: ")
student_class = input("Enter Class: ")
marks = float(input("Enter Marks: "))
add_student(roll_no, name, student_class, marks)
print("Student added successfully!")
elif choice == "3":
roll_no = int(input("Enter Roll No to update marks: "))
marks = float(input("Enter new marks: "))
update_marks(roll_no, marks)
print("Marks updated successfully!")

elif choice == "4":


roll_no = int(input("Enter Roll No to delete student: "))
delete_student(roll_no)
print("Student deleted successfully!")

elif choice == "5":


print("Exiting the system...")
break
else:
print("Invalid choice, please try again.")

if _name_ == "_main_":
main()

---

3. How the Program Works:

1. Connection to MySQL Database:

The connect_db() function connects to the MySQL server using the credentials
provided.

2. Fetching Data:

The fetch_students() function retrieves student records from the database and loads
them into a Pandas DataFrame.

The DataFrame allows for easy viewing and manipulation of the data.

3. Add, Update, Delete Operations:

Add student: Insert a new record into the students table.

Update marks: Update the marks for an existing student by Roll No.

Delete student: Delete a student record based on Roll No.

4. Main Menu System:

The user can choose between various options like viewing all students, adding a
student, updating marks, deleting a student, or exiting the program.
---

4. Sample Output:

Welcome to the Student Database System

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 1

Student Records:
RollNo Name Class Marks
0 101 Alice 12A 85.0
1 102 Bob 12B 72.0
2 103 Charlie 12A 91.0

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 2
Enter Roll No: 104
Enter Name: David
Enter Class: 12B
Enter Marks: 78
Student added successfully!

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 3
Enter Roll No to update marks: 102
Enter new marks: 80
Marks updated successfully!

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 4
Enter Roll No to delete student: 101
Student deleted successfully!

1. View all students


2. Add a new student
3. Update student marks
4. Delete a student
5. Exit
Enter your choice: 5
Exiting the system...
---

5. Limitations and Future Enhancements:

Limitations:

Single-threaded: The system is not designed for concurrent users.

No advanced features: The program lacks advanced functionalities such as attendance


tracking, class performance analysis, or reporting.

Basic validation: No input validation to handle incorrect inputs (e.g., entering a


string when numeric values are expected).

Future Enhancements:

GUI: A graphical user interface (e.g., using Tkinter) for a more user-friendly
experience.

Cloud Database: Storing the data on a cloud platform like Firebase for remote
access.

Advanced Reporting: Adding features to generate charts/graphs using libraries like


Matplotlib.

Security: Implementing user authentication to prevent unauthorized access.

---

Conclusion:

This project demonstrates how to connect Pandas with MySQL to create a simple yet
powerful student database management system. The use of Pandas allows for easy data
manipulation and analysis, while MySQL serves as a reliable backend for storing
student records. You can easily extend this project with additional features to
make it a comprehensive tool for school management.

You might also like