아보카도 Blog
TIL 19일차: 문제 해결 과정 본문
1. 여전히 수정한 댓글이 디비에 반영이 안되었다.
sql update와 ajax patch가 실행되는 순서는 다음과 같다.
파이썬 파일
@app.route("/edit", methods=["GET", "PATCH"])
def update_comment():
# render_template('edit.html')하는 첫 창
db = pymysql.connect(host='', user='', password='',db="", port=3306)
cursor = db.cursor()
sql = """
SELECT comment.comment_id, comment.comment_title, users.user_id,comment.comment_content,comment.star, comment.user_unique_id,comment.problem_id, comment.review_id
FROM comment
INNER JOIN users ON comment.user_unique_id =users.unique_id;
"""
cursor.execute(sql)
rows = cursor.fetchall()
json_str = json.dumps(rows, indent=4, sort_keys=True, default=str, ensure_ascii=False)
db.commit()
db.close()
# return json_str,200
#메소드가 PATCH일 때
if request.method == "PATCH":
comment_title = request.get_json().get("comment_title")
comment_content = request.get_json().get("comment_content")
star = request.get_json().get("star")
comment_id = request.get_json().get("comment_id")
#여기에서 씨퀄 오류가 난 것 같다. comment_id가 숫자인데, 문자열로 가져와서 그런것 같다.
sql = "UPDATE comment set comment_content = %s, comment_title = %s,star = %s where comment_id = %s"
cursor.execute(sql, (comment_content, comment_title, star, comment_id))
db.commit()
db.close()
return jsonify({'msg': '댓글이 정상적으로 수정되었습니다.'})
return render_template('edit.html')
한창 고치다가 팀에서 디비를 수정하는 바람에 튜터님의 도움이 중간에서 끊겼다.
다음에 다시 찾아가서 여쭤보는 걸로.... 일단은 프레젠테이션이나 만들고 있어야겠다.
2. 파이썬 파일에서 어떤 함수가 끝나고 리턴값을 정해줄 때 대부분을 render_template을 해줬다.
하지만 그 중 redirect로 설정해야 하는 부분이 있었다.
랜더 템플렛과 리다이렉트의 차이를 설명한 글
[Flask] redirect() vs render_template() 차이점, 다른점 🤷♂️
URL에 적어서 함수에게 302 헤더를 반환한다. 302 설명은 밑에서!보통 /a주소에서 데이터를 처리하는 과정만 거치고, 결과를 /b주소에서 보여주거나 할 때 응용된다.템플릿(.html)과 함께 200을 반환한
velog.io
3. 이제까지 사용했던 라이브러리 pymysql을 파이썬 파일에서 여러번 실행하고 나니 이 글이 쉽게 읽힌다.
https://velog.io/@jewon119/01.Mysql-%EA%B8%B0%EC%B4%88-pymysql-%EB%8B%A4%EB%A3%A8%EA%B8%B0-1
01.Mysql 기초 - pymysql 다루기
🌈 파이썬으로 mysql 다루기 > ### 🔥 pymysql 이란? > ### 🔥 pymysql로 데이터 생성(Create) > ### 🔥 pymysql로 데이터 읽기(Read) > ### 🔥 pymysql로 데이터 수정(Update) > ### 🔥 pymy
velog.io
그밖에 특강에서 배운 것을 잘 정리해주신 분의 글 링크
https://plucky-summer-2c4.notion.site/11-30-MySQL-163c7a0bccfe446cb7e18688d6b3052f
11.30 MySQL 실습 내용 -오전
DB, Table 생성, 수정, 삽입, 삭제 연습
plucky-summer-2c4.notion.site
'개발일지' 카테고리의 다른 글
TIL 20일차 (0) | 2022.12.09 |
---|---|
화면구현 프로젝트 KPT 회고 (0) | 2022.12.09 |
TIL 18일차: 문제 해결 과정 (0) | 2022.12.07 |
WIL 3주차: 첫번째 타임어택, 특강의 늪 DB, MySQL, restAPI, HTML, CSS특강 (1) | 2022.12.07 |
TIL 17일차: 문제 해결 과정 (get ajax에서 json값 불러오기, sql strict 디폴트값 없는 것 채워주기, console.log()로 셀프 디버깅) (1) | 2022.12.07 |