我正在尝试使用 if/elif 编写一个函数,我在尝试执行 elif 之后的最终游标函数时遇到了问题。我认为我的缩进是错误的,我现在一直在尝试找出错误的地方:
def api_report(request):
params = request.GET
if params["type"] == 'revenue':
sql = get_revenue_query(params)
elif params["type"] == 'order_count':
sql = get_order_created_count(params)
elif params["type"] == 'product_count':
sql = get_product_count(params)
elif params["type"] == 'order_card_created_count':
sql = get_order_card_created_count(params)
elif params["type"] == 'product_count':
sql = get_product_count(params)
elif params["type"] == 'card':
sql = get_card_query(params)
elif params["type"] == 'order_not_card_created_count':
sql = get_order_not_card_created_count(params)
elif params["type"] == 'product':
get_product_report(params)
elif params["type"] == 'order_rate_by_district':
sql = get_order_rate_by_district(params)
with connection.cursor() as cursor:
cursor.execute(sql)
rows = cursor.fetchall()
data = []
for row in rows:
data.append(OrderRateDataEntry(row[0], row[1], row[2]))
serializer = OrderRateDataEntrySerializer(data, many=True)
return JsonResponse(serializer.data, safe=False)
with connection.cursor() as cursor:
cursor.execute(sql)
rows = cursor.fetchall()
data = []
for row in rows:
data.append(TimeSeriesDataEntry(row[0], row[1]))
serializer = TimeSeriesDataEntrySerializer(data, many=True)
return JsonResponse(serializer.data, safe=False)
错误:
cursor.execute(sql) UnboundLocalError:
local variable 'sql' referenced before assignment
在elif params["type"] == 'product':和elif params["type"] == 'order_rate_by_district':有他们自己的函数来执行,我想其他条件与在代码的末尾跳转到最后光标功能。
慕工程0101907
红糖糍粑
蝴蝶刀刀
红颜莎娜
随时随地看视频慕课网APP
相关分类