ssk’s posterous

ssk’s posterous

Oct 25 / 9:27pm

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 形式へ?

0 comments

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter