python - ValueError on jsonify -


i new flask , python, have convert mysql query json format. on converting, facing value error , unable understand, please me, thankyou in advance.

valueerror: dictionary update sequence element #0 has length 8; 2 required on executing jsonify(r)

@app.route('/post') def display():   cursor.execute("select * tablename")   result=cursor.fetchall()   r = [dict((cursor.description[i][0], value) i, value in enumerate(row)) row in result]  return jsonify(r) 

there's better way create dictionaries mysql queries, , around error you're receiving.

when define cursor, should have return query results list of dictionaries, so:

cursor = connection.cursor(dictionary=true)

that way each row of table returned query exist dictionary within list. this:

[     {'firstcol': 'value1row1', 'secondcol': 'value2row1'... 'tenthcol': 'value10row1'},     {'firstcol': 'value1row2', 'secondcol': 'value2row2'... 'tenthcol': 'value10row2'},     ...     {'firstcol': 'value1row50', 'secondcol': 'value2row50'... 'tenthcol': 'value10row50'} ] 

then, jsonifying results becomes trivially easy:

return jsonify(result)

from there, have json object elements can referenced key. if want display values 'firstcol', need response['firstcol'] in javascript file.

also, make sure you're clear on flask routing, titling route '/post' not define 'post' method route. you'd need:

@app.route('/post' methods=['post'])

i apologize if aware of - started learning flask few months ago , pretty confused of conventions, imagine may in same boat was.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -