我使用sqlite3游标计算百分比变化的代码如下:
cursor3.execute('SELECT total FROM userTotals WHERE userid = :userid ORDER BY date DESC LIMIT 1', {'userid': userid})
for value in cursor3:
total = value
percentageChange = ((float(valueList['Total']) - total)/abs(total))*100
在这种情况下,float(valueList['Total'])没有问题,我从数据库中收集的总数是REAL类型的,sqlite命令返回的当前值是1.0。为什么会出现以下错误?你知道吗
percentageChange = ((float(valueList['Total']) - total)/abs(total))*100
TypeError: unsupported operand type(s) for -: 'float' and 'tuple'
似乎
total
持有一个类型为tuple
的值。我建议您打印查询返回的数据以查看实际包含的数据。print(cursor3)
或print(value)
应该可以。你知道吗您可能需要从cursor3中分离出两个不同的变量。我希望它是数据和查询相关的元数据,比如查询的成功状态或者类似的东西。(不过,最后一部分不太清楚,我自己也没试过。)
我相信您的游标正在返回一个元组,而值正在引用元组对象。你知道吗
您可以使用如下打印语句来确认这一点:
检查你到底得到了什么。你知道吗
另外,为什么不尝试使用以下语法:
查询返回一堆行。包含单个值的行与仅包含该值的行不同(例如
1 != [1]
),因此需要从中提取单个字段:或:
以上格式适用于具有多列的结果:
相关问题 更多 >
编程相关推荐