2

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='test') 

    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
4

1 回答 1

3

由于您没有在“尝试:”块之前声明“行”,因此如果出现异常,则不会定义它。尝试放在rows = []上面try

于 2012-09-06T21:36:47.620 回答