简体   繁体   English

UnboundLocalError:分配前已引用局部变量“ cur”

[英]UnboundLocalError: local variable 'cur' referenced before assignment

The following code throws the UnboundLocalError: 以下代码引发UnboundLocalError:

def fetch_results(conn, sql, **bind_params):
    """
    Immediately fetches the SQL results into memory
    Trades memory for the ability to immediately execute another query
    """
    global _log_func
    try:
        cur = conn.cursor()
        if _log_func:
            _log_func(cur, sql, bind_params)

        cur.execute(sql, bind_params)
        return cur.fetchall()
    finally:
        cur.close()

The error: 错误:

line 75, in fetch_results
    cur.close()
UnboundLocalError: local variable 'cur' referenced before assignment

I am not sure why. 我不知道为什么。 Could anybody point me to the right direction? 有人能指出我正确的方向吗?

If conn.cursor() throws an exception, cur will never be assigned, thus the code in the finally block will be referencing cur before assignment. 如果conn.cursor()引发异常,则将永远不会分配cur,因此,finally块中的代码将在分配之前引用cur。

Try removing that try block and seeing what happens, its likely that conn.cursor() is throwing some kind of exception you'll need to sort out. 尝试删除该try块并查看会发生什么,很可能conn.cursor()抛出了某种异常,您需要对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:调用WinDLL后在分配之前引用了本地变量'cur' - UnboundLocalError: local variable 'cur' referenced before assignment after call WinDLL UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment UnboundLocalError:赋值前引用了局部变量“Score” - UnboundLocalError: local variable 'Score' referenced before assignment UnboundLocalError:分配前引用了局部变量“检查” - UnboundLocalError: local variable 'checking' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM