드레:
코딩 뿌시기
드레:
전체 방문자
오늘
어제
  • 분류 전체보기 (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)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
드레:

코딩 뿌시기

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

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

2023. 1. 4. 20:00

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

	# PUT 메소드를 처리하는 함수
	def put(self, recipe_id):
       
        data = request.get_json()

        try:
            connection = get_connection()
            
            query = '''update recipe
                    set
                    name = %s,
                    description = %s,
                    num_of_servings = %s,
                    cook_time = %s,
                    directions = %s
                    where id = %s;'''

            record = (data['name'], 
                      data['description'],
                      data['num_of_servings'],
                      data['cook_time'],
                      data['directions'],
                      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' : 'fall',
                    '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 만들기  (2) 2023.01.05
API서버 - Python MySQL Connector를 이용해 delete하기(DELETE 메소드)  (0) 2023.01.04
API서버 - Python MySQL Connector를 이용해 select하기(GET 메소드)  (0) 2023.01.04
API서버 - Python MySQL Connector를 이용해 insert하기(POST 메소드)  (0) 2023.01.04
Python Flask를 사용한 REST API서버 개발방법  (0) 2023.01.04
    'REST API' 카테고리의 다른 글
    • API서버 - Flask JWT를 사용한 회원가입 API 만들기
    • API서버 - Python MySQL Connector를 이용해 delete하기(DELETE 메소드)
    • API서버 - Python MySQL Connector를 이용해 select하기(GET 메소드)
    • API서버 - Python MySQL Connector를 이용해 insert하기(POST 메소드)
    드레:
    드레:

    티스토리툴바