UnboundLocalError: local variable 'rows' referenced before assignment

Posted by patrick on Stack Overflow See other posts from Stack Overflow or by patrick
Published on 2012-09-06T21:31:42Z Indexed on 2012/09/06 21:38 UTC
Read the original article Hit count: 702

i'm trying to make a database connection by an other script. But the script didn't work propperly.

and if I do a 'print' on the rows then I get the value 'null'

But if I use a 'select * from incidents' query then i get the result from the table incidents.

import database

rows = database.database("INSERT INTO incidents VALUES(3 ,'test_title1', 'test',     TO_DATE('25-07-2012', 'DD-MM-YYYY'), CURRENT_TIMESTAMP, 'sector', 50, 60)")
#print database.database()
print rows

database.py script:

import psycopg2
import sys
import logfile

def database(query):
logfile.log(20, 'database.py', 'Executing...')  
con = None

try:        
    con = psycopg2.connect(database='incidents', user='ipfit5', password='tester') 

    cur = con.cursor()
    #print query

    cur.execute(query)
    rows = cur.fetchall()

    con.commit()


#test row does work
    #cur.execute("INSERT INTO incidents VALUES(3 ,'test_titel1', 'test', TO_DATE('25-07-2012', 'DD-MM-YYYY'), CURRENT_TIMESTAMP, 'sector', 50, 60)")

except:
    logfile.log(40, 'database.py', 'Er is iets mis gegaan') 
    logfile.log(40, 'database.py', str(sys.exc_info()))

finally:

    if con:
        con.close()
return rows

© Stack Overflow or respective owner

Related posts about python

Related posts about query