UnboundLocalError: local variable 'xx' referenced before assignment的问题与解决
2019-03-24 18:30
387 查看
-
问题描述
在写爬虫连接数据库这一操作的时候,系统报了以下错误:
“UnboundLocalError: local variable ‘gamedata’ referenced before assignment”
具体报错如下图所示:
自己百度了一下,大多是说没有明确该变量是全局变量还是局部变量,从而导致了问题的出现。我参照了他们提供的解决方法尝试了一下,没能解决,再说我之前也写过了类似的代码,而且没有报错,所以我就打算自己再调试一波。
-
出错代码
def read_gamedata(): # 连接数据库 try: conn = pymysql.connect(host='127.0.0.1', user='root', passwd='root', db='RecSystem', port=3306, charset='utf8') cur = conn.cursor() try: sql = "select url from gameData;" cur.execute(sql) gamedata = cur.fetchmany(2) except Exception as e: print(e) cur.close() conn.close() except Exception as e: print(e) return gamedata
根据报错信息定位到了这个函数,本来以为是在数据表中添加了新的字段才导致的,然后我单独执行了这一个函数之后发现:
Emmm…
然后我又跑到数据库那查看数据表的信息,发现不知道什么时候这多了个空格。。。
好吧,我把空格删除了之后代码就可以执行了。。。
至此,问题解决~
相关文章推荐
- 出现UnboundLocalError: local variable 'a' referenced before assignment异常的情况与解决方法
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- Python 读写当前路径下文件错误 UnboundLocalError: local variable 'file' referenced before assignment
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- 全局变量报错:UnboundLocalError: local variable 'i' referenced before assignment
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- UnboundLocalError: local variable 'photoshop' referenced before assignment
- python:UnboundLocalError: local variable 'l' referenced before assignment
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- python文件运行报错:UnboundLocalError: local variable 'status' referenced before assignment
- Python错误:UnboundLocalError: local variable 'total' referenced before assignment解决办法
- UnboundLocalError: local variable 'epoch_logs' referenced before assignment
- RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment
- 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
- UnboundLocalError: local variable 'c' referenced before assignment
- Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
- Python全局变量global和局部变量 UnboundLocalError: local variable 'x' referenced before assignment
- 【Python】UnboundLocalError: local variable 'x' referenced before assignment