在下面的代码中,我有两个游标,第一个loc_crs应该有3行。但是当我运行我的程序时,loc_crs似乎只有1行
import db_access
def get_average_measurements_for_area(area_id):
"""
Returns the average value of all measurements for all locations in the given area.
Returns None if there are no measurements.
"""
loc_crs = db_access.get_locations_for_area(area_id)
avg = 0
for loc_row in loc_crs:
loc_id = int(loc_row['location_id'])
meas_crs = db_access.get_measurements_for_location(loc_id)
for meas_row in meas_crs:
meas = float(meas_row['value'])
avg += meas
# print("meas: ", str(meas))
print("loc_id: ", str(loc_id))
print(str(avg))
get_average_measurements_for_area(3)
这是我的输出。
loc_id: 16
avg: 568.2259127787871
编辑:以下是被调用的方法:
def get_locations_for_area(area_id):
"""
Return a list of dictionaries giving the locations for the given area.
"""
cmd = 'select * from location where location_area is ?'
crs.execute(cmd, [area_id])
return crs
def get_measurements_for_location(location_id):
"""
Return a list of dictionaries giving the measurement rows for the given location.
"""
cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
return crs
以下是数据库的链接: http://cs.kennesaw.edu/~bsetzer/4320su15/extra/databases/measurements/measures.sqlite