Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
NotificationsYou must be signed in to change notification settings

burkaslarry/python_mysql_http

Repository files navigation

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.

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 venvsource 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=localhostDB_PORT=3306DB_USER=your_usernameDB_PASSWORD=your_passwordDB_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

[8]ページ先頭

©2009-2025 Movatter.jp