亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

賦值前引用的局部變量“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 關注
  • 239 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號