2015-06-18 67 views
1

我對python相當陌生,無法在任何地方找到我的問題的答案。除非我不明白給出的答案。 我有一個數據庫遊標,我執行以下命令:從Python中調用fetchone()調用的值3

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

它打印:

{'value': 73.97486139568466} 

我需要使用浮動73.97 ....在一些計算來計算的平均值。 我的問題是,我不知道如何從fetchone()返回拉浮動。

回答

2

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) 

或任何其他的東西,你想這個結果

+0

你也可以使用'row.get(「值」)做' –