드레:
코딩 뿌시기
드레:
전체 방문자
오늘
어제
  • 분류 전체보기 (268)
    • Python (74)
      • Python 기초 (42)
      • Numpy (8)
      • Pandas (22)
    • Machine Learning (31)
      • Machine Learning (1)
      • Deep Learning (27)
    • AWS (22)
      • RDS (3)
      • EC2 (9)
      • Lambda (8)
      • S3 (2)
    • MySQL (24)
    • Git (8)
    • Streamlit (12)
    • REST API (22)
    • Java (24)
    • Android (36)
    • Debugging (15)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 액션바
  • Lambda
  • AWS
  • github
  • rest api
  • EC2
  • Transfer Learning
  • GET
  • volley
  • fine tuning
  • 안드로이드 스튜디오
  • Streamlit
  • tensorflow
  • CNN
  • pandas
  • AWS Lambda
  • Ann
  • flask
  • aws s3
  • JWT
  • Java
  • 딥러닝
  • serverless
  • 네이버 API
  • 서버리스
  • 깃이그노어
  • Callback
  • API
  • Retrofit2
  • Python

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
드레:

코딩 뿌시기

API서버 - Python MySQL Connector를 이용해 delete하기(DELETE 메소드)
REST API

API서버 - Python MySQL Connector를 이용해 delete하기(DELETE 메소드)

2023. 1. 4. 20:05

mysql_connection.py 파일

# MySQL에 접속하는 함수
import mysql.connector

from config import Config 

def get_connection():

    connection = mysql.connector.connect(
        host= Config.HOST,
        database= Config.DATABASE,
        user= Config.DB_USER,
        password= Config.DB_PASSWORD
    )
    return connection

 

 

API 리소스를 관리하는 recipe.py 파일

from flask import request
from flask_restful import Resource
from mysql_connection import get_connection
import mysql.connector

# API를 만들기 위해서는, flask_restful 라이브러리의 Resource 클래스를 상속해서 만든다.
class RecipeResource(Resource) :
	
    # DELETE 메소드를 처리하는 함수
    def delete(self, recipe_id):
        
        try:
            connection = get_connection()
            query = '''delete from recipe
                    where id = %s;'''
            record = (recipe_id,)

            cursor = connection.cursor()
            cursor.execute(query, record)

            connection.commit()

            cursor.close()
            connection.close()
        
        except mysql.connector.Error as e :
            print(e)
            cursor.close()
            connection.close()
            return {"result" : "fail", "error" : str(e)}, 500

        return {'result' : 'success'}, 200

 

 

메인파일 app.py

from flask import Flask
from flask_restful import Api
from config import Config

from resources.recipe import RecipeListResource, RecipeResource, RecipePublishResource
from resources.user import UserRegisterResource



app = Flask(__name__)
# 환경변수 셋팅
app.config.from_object(Config)

api = Api(app)

# 경로와 리소스를 연결한다.
api.add_resource(RecipeListResource, '/recipes')
api.add_resource(RecipeResource, '/recipes/<int:recipe_id>')
api.add_resource(RecipePublishResource, '/recipes/<int:recipe_id>/publish')

api.add_resource(UserRegisterResource, '/user/register')


if __name__ == '__main__':
    app.run()

 

 

포스트맨에서 테스트

 

 

MySQL에서 확인

select * from recipe;

정상적으로 삭제되었다.

'REST API' 카테고리의 다른 글

API서버 - Flask JWT를 사용한 로그인 API 만들기  (0) 2023.01.05
API서버 - Flask JWT를 사용한 회원가입 API 만들기  (2) 2023.01.05
API서버 - Python MySQL Connector를 이용해 update하기(PUT 메소드)  (0) 2023.01.04
API서버 - Python MySQL Connector를 이용해 select하기(GET 메소드)  (0) 2023.01.04
API서버 - Python MySQL Connector를 이용해 insert하기(POST 메소드)  (0) 2023.01.04
    'REST API' 카테고리의 다른 글
    • API서버 - Flask JWT를 사용한 로그인 API 만들기
    • API서버 - Flask JWT를 사용한 회원가입 API 만들기
    • API서버 - Python MySQL Connector를 이용해 update하기(PUT 메소드)
    • API서버 - Python MySQL Connector를 이용해 select하기(GET 메소드)
    드레:
    드레:

    티스토리툴바