【问题标题】:Python PostGreSQL query doesn't return plain number [duplicate]Python PostGreSQL 查询不返回纯数字 [重复]
【发布时间】:2015-10-29 19:57:39
【问题描述】:

我正在运行 PostGreSQL 查询,当我打印它时,会返回:[{'sum': 117L}]

这是代码本身:

cursor.execute("SELECT SUM(length) FROM carmileage")
totalLength = cursor.fetchall()
print totalLength

如果没有围绕 JSON 的(看起来是什么),我如何将其格式化为数字?

【问题讨论】:

标签: python sql postgresql


【解决方案1】:

只需遍历您的结果集。游标将 SQL 选择查询中的行传递到 Python 列表中:

cursor.execute("SELECT SUM(length) FROM carmileage")
totalLength = cursor.fetchall()

for row in totalLength:
    print(row)

【讨论】:

  • 我发现最适合我的解决方案是使用cursor.fetchone()['sum']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
相关资源
最近更新 更多