我使用 python 和 mySql,运行下面的查询:
sSql = "select id from table_name"
cursor.execute( sSql )
lrec = self.cursor.fetchall()
json.dumps( lrec )
并收到一条错误消息,因为我返回的是 long int notation 'id' : 1L
而不是 'id' : 1
我发现解决这个问题的唯一方法是丑陋的:
sSql = "select cast(id as char(10)) as id from table_name"
有什么更好的方法吗?
谢谢, 于尔根
最佳答案
您可以遍历 lrec 并将所有 id
转换为 int
。我不知道你的 lrec 是如何格式化的(即元组列表)。
no_longs = [int(my_id) for my_id in lrec]
关于python - json.dump python mysql 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17451514/