Skip to content

burkaslarry/python_mysql_http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Here's a reformatted README for your Python MySQL project using FastAPI:

Python MySQL FastAPI Project

This is a sample project demonstrating how to use FastAPI with MySQL, featuring CRUD operations and asynchronous database connections.

Features

  • FastAPI web framework
  • MySQL database integration using aiomysql
  • Environment variable configuration for database connection
  • Basic CRUD operations
  • "Hello World" JSON endpoint for testing

Setup

Step 1: Create a Virtual Environment and Install Dependencies

python -m venv venv
source venv/bin/activate   # On Windows use `venv\Scripts\activate`
pip install fastapi uvicorn aiomysql aiohttp python-dotenv

Step 2: Create a .env File for Database Configuration

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

Step 3: Install and Run Uvicorn

Install Uvicorn:

pip install uvicorn

Run the application:

uvicorn main:app --reload

Testing CRUD Operations

You can test the CRUD operations using curl or any API testing tool like Postman:

Create an Item:

curl -X POST "http://127.0.0.1:8000/items" -H "Content-Type: application/json" -d '{"name": "Item 1", "description": "Description 1"}'

Read an Item:

curl "http://127.0.0.1:8000/items/{id}"

Read a List of Items (Offset and Limit):

curl "http://127.0.0.1:8000/items?limit={limit}&offset={offset}"

Update an Item:

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"}'

Delete an Item:

curl -X DELETE "http://127.0.0.1:8000/items/{id}"

Project Structure

  • main.py: Contains the FastAPI application and route definitions
  • .env: Stores database configuration (not tracked in version control)
  • requirements.txt: Lists all Python dependencies

Notes

  • 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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published