Development/Flask with Python
[Flask1.1][Mysql] Query Parameter 적용하기 예시
Best Junior
2020. 1. 22. 01:24
[ 작업 환경 ]
# vim app001/routes.py
# ...
@app.route('/stations')
def get_stations_from_name():
station_name = request.args.get('name')
result = Location.station_informations_from_name(station_name)
if result:
return fsonify(result)
return jsonify(message="Ain't no cake like that.")
# vim app001/models
# ...
def station_informations_from_name(name):
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT * FROM stations WHERE name LIKE '%" + name + "%'")
result = cursor.fetchall()
if result:
return result
return False
[ 결과 예시 ]
만약 한글 깨짐 등의 문제로 정상 출력이 되지 않는다면, 아래 링크를 참조하여 해결할 수 있습니다.
https://growingsaja.tistory.com/354