在奇怪的条件下赋值之前引用的局部变量

时间:2015-06-08 14:06:29

标签: mysql eclipse python-2.7 mysql-python

我有一些代码从开源数据库获取输入,然后根据某些表返回报告。我本可以发誓这个代码昨天正常工作,但今天我启动它时:

 
Traceback (most recent call last):
  File "C:\Users\whicaso1\Downloads\eclipse-standard-luna-R-win32-x86_64\eclipse\plugins\org.python.pydev_3.6.0.201406232321\pysrc\pydevd.py", line 1845, in 
    debugger.run(setup['file'], None, None)
  File "C:\Users\whicaso1\Downloads\eclipse-standard-luna-R-win32-x86_64\eclipse\plugins\org.python.pydev_3.6.0.201406232321\pysrc\pydevd.py", line 1373, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Users\whicaso1\workspace\Status based chilli list\CreateChiliLog.py", line 29, in 
    CreateChiliLog()
  File "C:\Users\whicaso1\workspace\Status based chilli list\CreateChiliLog.py", line 15, in CreateChiliLog
    for chili in chili_database.getChilis(status):
  File "C:\Users\whicaso1\workspace\Status based chilli list\ChiliFunctions.py", line 99, in getChilis
    connection_chili.rollback()
UnboundLocalError: local variable 'connection_chili' referenced before assignment

现在,我不是白痴,我查看是否有,实际上,引用了变量" connection_chili"在分配之前,但看起来分配时发生了错误。 (这里是失败的代码片段,错误位于将connection_chili变量分配给mysqldb数据库对象的行上。)

 
def getChilis(self, status):
        try:
            connection_chili = MySQLdb.connect(self.host_, self.user_, self.password_, self.database_)
            cursor = connection_chili.cursor()
            query = "SELECT id FROM issues WHERE status_id = %i" % status
            cursor.execute(query)
            connection_chili.commit()
            chilis = cursor.fetchall()
            cursor.close()
            return chilis

    except Exception, e:
        connection_chili.rollback()
        print "ERROR: " + str(e)

except Exception, e: connection_chili.rollback() print "ERROR: " + str(e)

我的代码是否有问题,或者我的python编译器是否有一些奇怪的事情?

1 个答案:

答案 0 :(得分:0)

刚刚在cmd中针对python检查了我的代码,它运行正常。因此,我猜测它可能与eclipse有些奇怪的问题。无论如何,它不再是一个编程问题,所以我要关闭它