[英]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.