Notice
Recent Posts
Recent Comments
Link
뭐야 왜 an 돼
Python MySQL Connector update 본문
포스트맨을 실행시켜 PUT으로 설정하고 URL을 입력해준뒤
raw JSON형식으로 DB에 업데이트할 정보를 입력해주세요.

Visual Studio Code에서 아래처럼 코드를 작성해줍니다
+ app.py에 경로와 리소스(API코드)를 연결하는 코드도 작성해세요
>>> api.add_resource(RecipeResource, '/recipes/<int:recipe_id>')
class RecipeResource(Resource) :
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 Error as e :
print(e)
cursor.close()
connection.close()
return {"result" : "fail", "error" : str(e)}, 500
return {"result" : "success"}, 200
데이터가 업데이트 되었는지 MySQL Workbench에서 확인해주세요


'RESTful API' 카테고리의 다른 글
| API, serverless 와 깃허브 Actions로 AWS 자동 배포 설정 하기 (0) | 2024.05.24 |
|---|---|
| Python MySQL Connector Delete (0) | 2024.05.21 |
| RESTful API로 레시피 목록 가져오기: Python과 Flask 활용(Selecet) (0) | 2024.05.21 |
| Python을 사용한 MySQL 데이터베이스 연결 설정 (0) | 2024.05.21 |
| Python MySQL Connector insert 하는 방법 (Flask,Post man) (0) | 2024.05.21 |