arbiter's dream place in Utah: Python and MySQL technique - Render JSON ouput based on MySQLdb to MySQL
by ssk
Here is the code.
import MySQLdb
import simplejson as json
db = MySQLdb.connect(host='xxx', user='xx', passwd='xx', db='xxx')
cursor = db.cursor()
def getSQLJSON(hostName,dbName,id,pw,sql):
global db,cursor
resultArray =[]
cursor.execute(sql)
tableDescription = cursor.description
result = cursor.fetchall()
for e in result:
resultRaw ={}
columnIndex =0
for ee in e:
vcoulumnName = tableDescription [columnIndex][0]
resultRaw[coulumnName]=ee
columnIndex +=1
resultArray.append(resultRaw)
jsonResult = json.dumps(resultArray, separators=(',',':'))
return jsonResult
mysql の出力を JSON 形式へ?

