Reputation: 315
I am fairly new to python and I can not find the answer to my question anywhere. Unless I just don't understand the answers given. I have a database cursor and I execute this command:
cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
print(crs.fetchone())
Which prints:
{'value': 73.97486139568466}
I need to use the float 73.97.... in some calculations to compute an average. My problem is that I can't figure out how to pull the float from the fetchone() return.
Upvotes: 2
Views: 7392
Reputation: 1
You can do something like below:
row=crs.fetchone()
for x in row:
print(x)
Value=x
you can use value variable in your calculations
Upvotes: -2
Reputation: 14520
fetchone
is returning you a dictionary for each row mapping the field name in the result (only value
in this example) with the value for that row. To use it you can do
cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
row = crs.fetchone()
print(row['value'])
print(row['value'] * 100)
or whatever other things you want to do with that result
Upvotes: 6