A simple Python application that demonstrates CRUD (Create, Read, Update, Delete) operations with a MySQL database for user management.
- Create Users: Add new users with name, email, and age
- Read Users: Display all users from the database
- Update Users: Modify existing user information
- Delete Users: Remove users from the database
- Count Users: Get total number of users for statistics
- Python 3.x
- MySQL Server
mysql-connector-pythonpackage
-
Clone this repository or download the files
-
Install the required Python package:
pip install mysql-connector-python
-
Set up MySQL database:
- Start your MySQL server
- Run the SQL commands from
database.sqlto create the database and table
Execute the following SQL commands in your MySQL client:
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(240) NOT NULL,
email VARCHAR(200) UNIQUE NOT NULL,
age INT NOT NULL
);Update the database connection parameters in main.py:
connection = mysql.connector.connect(
host="localhost",
user="root",
password="", # Add your MySQL password here
database="testdb"
)The application provides the following functions:
create_user("John Doe", "john@example.com", 30)read_users()update_user(1, name="Jane Doe", email="jane@example.com", age=25)delete_user(1)total_users = count_users()
print(total_users)├── main.py # Main application with CRUD functions
├── database.sql # Database schema and setup
└── README.md # This file
- Ensure MySQL server is running
- Create the database using
database.sql - Update connection parameters in
main.py - Run the application:
python main.py
- Uncomment the function calls at the end of
main.pyto test the functionality - Make sure to update the database connection credentials before running
- The application currently prints the total user count when executed
- Update the database password in the connection configuration
- Consider using environment variables for sensitive database credentials
- The current implementation has basic SQL injection protection for some operations
This project is for educational purposes and demonstrates basic database operations with Python and MySQL.