0% found this document useful (0 votes)
6 views9 pages

Python CRUD Operations with MySQL

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

Python CRUD Operations with MySQL

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

Step 1: Install MySQL Connector

First, you need to install the MySQL connector for Python. Run the following command in your
terminal or command prompt:

Step 2: Create a MySQL Database and Table

Create a database and a table in MySQL. You can use the following SQL commands:

Step 3: Python Code for CRUD Operations

Now, here's a Python script to connect to the MySQL database and perform basic CRUD
(Create, Read, Update, Delete) operations.
Explanation of Functions:

1. add_student(): Adds a new student record to the database.


2. view_students(): Retrieves and displays all student records from the database.
3. update_student(): Updates an existing student record based on the student's ID.
4. delete_student(): Deletes a student record from the database based on the student's
ID.

Running the Program

1. Make sure your MySQL server is running and you have created the database and table.
2. Run the Python script. It will prompt you to choose an operation (Add, View, Update, or
Delete a student).
3. Follow the instructions for each option.

Output
1. Add a Student:
2. View All Students:

3. Update a Student:
4. Delete a Student:

5. Exit the Program:

You might also like