Du kan följa det här svaret:https://stackoverflow.com/a/41388992/3129414
Befintlig kodändring:
- använd
DictCursor
iMySQL
initiering - ta bort
dictionary=True
från markören - använd
json
paket för att konvertera ordboksobjekt till json-format
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)
Skärmdump: