local variable referenced before assignment in strange condition

829 views Asked by At

I have some code that takes input from an open source database, then returns a report based on some of the tables. I could have sworn that this code was working correctly yesterday, but when I boot it up today:

 
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

Now, I'm not a moron, I checked to see if I had, in fact, referenced the variable "connection_chili" before assignment, but it looks like the error occurred while assigning. (here is the code snippet that failed, the error is on the line where the connection_chili variable is being assigned to a mysqldb database object.)

 
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)

is there some issue with my code, or is there some weird thing going on with my python compiler?

1

There are 1 answers

0
logicNoob On BEST ANSWER

Just checked my code against python in cmd, and it ran fine. So I'm guessing its probably some weird issue with eclipse. In any case, its no longer a programming question, so I'm going to close it