从 python 3 中的 fetchone() 调用中检索值

Cha*_*Jr. 2 python sqlite

我对 python 相当陌生,我在任何地方都找不到我的问题的答案。除非我只是不明白给出的答案。我有一个数据库游标,我执行这个命令:

cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
print(crs.fetchone())
Run Code Online (Sandbox Code Playgroud)

哪个打印:

{'value': 73.97486139568466}
Run Code Online (Sandbox Code Playgroud)

我需要在某些计算中使用浮点数 73.97.... 来计算平均值。我的问题是我不知道如何从 fetchone() 返回中提取浮点数。

Eri*_*ouf 5

fetchone正在为每一行返回一个字典,将结果中的字段名称(仅value在此示例中)与该行的值进行映射。要使用它,你可以这样做

cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
row = crs.fetchone()
print(row['value'])

print(row['value'] * 100)
Run Code Online (Sandbox Code Playgroud)

或者你想用这个结果做的任何其他事情