mysql - Displaying database results in Python Flask: ValueError: dictionary update sequence element #0 has length 6; 2 is required -
how display results of database query out in cocoarestclient using python flask?
here's code:
import json import sys import datetime import mysqldb mdb flask import flask, request, jsonify app = flask(__name__) @app.route('/home') def hello_world(): return "welcome omnimoda." @app.route('/items/list') def get_items(): #change accordingly: #connection details: hostname = "localhost" username = "un" password = "pw" database = "db" q_list_one = "select * item_info" con = mdb.connect(hostname, username, password, database) cur = con.cursor() json_return = {} try: cur.execute(q_list_one) r_list_one = cur.fetchall() except error error: print(error) finally: cur.close() con.close() return jsonify(r_list_one) if __name__ == '__main__': app.run(host = '0.0.0.0', debug=true)
i got jsonify
bit here, i'm getting valueerror: dictionary update sequence element #0 has length 6; 2 required
(full error traceback here)
without jsonify
, typeerror: 'tuple' object not callable
.
here's database looks like:
anything else can try? thanks.
you using jsonify
wrong. you'll need use keyword argument, in answer link refered to:
return jsonify(data=r_list_one)
the official doc gives example:
return jsonify(username=g.user.username, email=g.user.email, id=g.user.id)
above code results in response this:
{ "username": "admin", "email": "admin@localhost", "id": 42 }
so can see keywords necessary when using jsonify
.
Comments
Post a Comment