Skip to content

bouzayenilyes/python_mysql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

User Management System

A simple Python application that demonstrates CRUD (Create, Read, Update, Delete) operations with a MySQL database for user management.

Features

  • 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

Prerequisites

  • Python 3.x
  • MySQL Server
  • mysql-connector-python package

Installation

  1. Clone this repository or download the files

  2. Install the required Python package:

    pip install mysql-connector-python
  3. Set up MySQL database:

    • Start your MySQL server
    • Run the SQL commands from database.sql to create the database and table

Database Setup

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
);

Configuration

Update the database connection parameters in main.py:

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",  # Add your MySQL password here
    database="testdb"
)

Usage

The application provides the following functions:

Create a User

create_user("John Doe", "john@example.com", 30)

Read All Users

read_users()

Update User Information

update_user(1, name="Jane Doe", email="jane@example.com", age=25)

Delete a User

delete_user(1)

Count Total Users

total_users = count_users()
print(total_users)

File Structure

├── main.py          # Main application with CRUD functions
├── database.sql     # Database schema and setup
└── README.md        # This file

Running the Application

  1. Ensure MySQL server is running
  2. Create the database using database.sql
  3. Update connection parameters in main.py
  4. Run the application:
    python main.py

Notes

  • Uncomment the function calls at the end of main.py to test the functionality
  • Make sure to update the database connection credentials before running
  • The application currently prints the total user count when executed

Security Considerations

  • 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

License

This project is for educational purposes and demonstrates basic database operations with Python and MySQL.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages