[英]pass data from mySQL to an api in Flask
我需要使用 Flask 在 Python 中將數據從 mySQL 轉換為 JSON。 我直接在 mySQL 中使用轉換,但它給了我錯誤。 這是代碼。
@app.route('/getdata')
def getdata():
cnx = mysql.connector.connect(**app.connDBConfig)
cursor = cnx.cursor()
query = "SELECT JSON_ARRAYAGG(JSON_OBJECT('nome', columnName, 'cognome', columnSur,)) from table;"
cursor.execute(query)
data = ""
for elem in cursor:
data = elem
return jsonify(data)
這給了我一個奇怪的 JSON
[ "[{"nome": "enry", "cognome": "ford"}, {"nome": "michel", "cognome": "rodriguez"}, {"nome": "paul", "cognome ": "roger"}, {"nome": "marcial", "cognome": "corz"}]" ]
為什么會這樣,我該如何解決?
編輯
如果我使用 fethall 它總是給我一個唯一對象中的所有 JSON
您應該使用cursor.fetchall()
或cursor.fetchmany()
從游標中獲取數據。 文檔在這里
游標對象不是數據! ,它只是一個幫助您獲取數據的處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.