为了账号安全,请及时绑定邮箱和手机立即绑定

赋值前引用的局部变量“sql”

赋值前引用的局部变量“sql”

慕工程0101907 2021-12-09 15:57:12
我正在尝试使用 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':有他们自己的函数来执行,我想其他条件与在代码的末尾跳转到最后光标功能。
查看完整描述

3 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

一旦你运行程序,这就是我假设会发生的事情(阅读 #)


def api_report(request):

    params = request.GET

    if params["type"] == 'revenue': # False so sql is not made, move to next elif

        sql = get_revenue_query(params)


    elif params["type"] == 'order_count': # False so sql is not made, move to next elif

        sql = get_order_created_count(params)


    elif params["type"] == 'product_count': # False so sql is not made, move to next elif

        sql = get_product_count(params)


    elif params["type"] == 'order_card_created_count': # False so sql is not made, move to next elif

        sql = get_order_card_created_count(params)


    elif params["type"] == 'product_count': # False so sql is not made, move to next elif

        sql = get_product_count(params)


    elif params["type"] == 'card': # False so sql is not made, move to next elif

        sql = get_card_query(params)


    elif params["type"] == 'order_not_card_created_count': # False so sql is not made, move to next elif

        sql = get_order_not_card_created_count(params)


    elif params["type"] == 'product': # False so sql is not made, move to next elif

        get_product_report(request) # P.S There is also a chance that if this is run then sql variable will also not be made!


    elif params["type"] == 'order_rate_by_district':  # This is also false so code leaves.

        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)


        pass

    # When the code is here it still didn't made variable sql. Thus so will crashes when refere to variable sql as it wasn't yet created

    with connection.cursor() as cursor:

        cursor.execute(sql) # sql was never made here and thus doesn't exist. Code crashes here.

        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)

Maby 在第一个 if 语句之前 make 并清空 sql 变量。(或任何您喜欢的默认值)


查看完整回答
反对 回复 2021-12-09
?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

您应该重新排列 if 序列以忽略sql空时的情况。否则你可以sql = 'some default value'在它上面添加,但它已经很难阅读了。


查看完整回答
反对 回复 2021-12-09
?
红颜莎娜

TA贡献1842条经验 获得超13个赞

在我改变了


 elif params["type"] == 'product':

      get_product_report(request)


 elif params["type"] == 'product': 

      return get_product_report(params)

它起作用是因为 get_product_report 是它自己的函数所以没有任何返回结果到 param = 'product' 条件所以它是错误的产品参数行(返回无)


查看完整回答
反对 回复 2021-12-09
  • 3 回答
  • 0 关注
  • 275 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号