Here's a reformatted README for your Python MySQL project using FastAPI:
This is a sample project demonstrating how to use FastAPI with MySQL, featuring CRUD operations and asynchronous database connections.
- FastAPI web framework
- MySQL database integration using aiomysql
- Environment variable configuration for database connection
- Basic CRUD operations
- "Hello World" JSON endpoint for testing
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install fastapi uvicorn aiomysql aiohttp python-dotenv
Create a file named .env
in your project directory and add your database connection details:
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database_name
Install Uvicorn:
pip install uvicorn
Run the application:
uvicorn main:app --reload
You can test the CRUD operations using curl or any API testing tool like Postman:
curl -X POST "http://127.0.0.1:8000/items" -H "Content-Type: application/json" -d '{"name": "Item 1", "description": "Description 1"}'
curl "http://127.0.0.1:8000/items/{id}"
curl "http://127.0.0.1:8000/items?limit={limit}&offset={offset}"
curl -X PUT "http://127.0.0.1:8000/items/{id}" -H "Content-Type: application/json" -d '{"id": {id}, "name": "Updated Item", "description": "Updated Description"}'
curl -X DELETE "http://127.0.0.1:8000/items/{id}"
main.py
: Contains the FastAPI application and route definitions.env
: Stores database configuration (not tracked in version control)requirements.txt
: Lists all Python dependencies
- This setup uses FastAPI for the web framework
- aiomysql is used for asynchronous MySQL connections
- Environment variables manage sensitive database connection details
- CRUD operations are performed using basic SQL queries
- The "Hello World" JSON endpoint serves as a simple test
Happy coding!