Vous pouvez suivre cette réponse :https://stackoverflow.com/a/41388992/3129414
Modification du code existant :
- utiliser
DictCursor
dansMySQL
initialisation - supprimer
dictionary=True
du curseur - utiliser
json
package pour convertir les éléments du dictionnaire au format json
app.py
:
import json
from flask import Flask
from flaskext.mysql import MySQL
from pymysql.cursors import DictCursor
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '123'
app.config['MYSQL_DATABASE_DB'] = 'practice_db'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql = MySQL(cursorclass=DictCursor)
mysql.init_app(app)
conn = mysql.connect()
@app.route("/", methods=['GET'])
def home():
email_address = '[email protected]'
cursor = conn.cursor();
cursor.execute(
'SELECT * FROM user_table WHERE email = (%s)', (email_address,)
)
users = cursor.fetchall()
return json.dumps(users)
app.run(debug=True)
Capture d'écran :