Class 12 Ip Radha
Class 12 Ip Radha
---
First, you need to set up a MySQL database and a table to store the student
records.
mysql -u root -p
3. Create a students table with fields like RollNo, Name, Class, Marks:
---
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
# 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()
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")
if choice == "1":
df = fetch_students()
print("\nStudent Records:")
print(df)
if _name_ == "_main_":
main()
---
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.
Update marks: Update the marks for an existing student by Roll No.
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:
Student Records:
RollNo Name Class Marks
0 101 Alice 12A 85.0
1 102 Bob 12B 72.0
2 103 Charlie 12A 91.0
---
Limitations:
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.
---
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:
---
First, you need to set up a MySQL database and a table to store the student
records.
mysql -u root -p
3. Create a students table with fields like RollNo, Name, Class, Marks:
---
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
# 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()
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")
if choice == "1":
df = fetch_students()
print("\nStudent Records:")
print(df)
if _name_ == "_main_":
main()
---
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.
Update marks: Update the marks for an existing student by Roll No.
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:
Student Records:
RollNo Name Class Marks
0 101 Alice 12A 85.0
1 102 Bob 12B 72.0
2 103 Charlie 12A 91.0
Limitations:
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.
---
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.