Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cursort.execute/cursor.callproc returns no error but nothing is executed

When i run this SQL query via psql client it runs for several seconds (~90 seconds, that's normal since its a huge table) and it return, then i can check that my line is successfully inserted.

SELECT merge_data('898989', '111111111', '10000')

It is a stored procedure that runs an UPDATE or INSERT, the procedure is ran without errors and i get my entry in the table.

When trying to do the same from a python program, the query take 2 seconds and returns no errors, i get nothing in my table; for information, the statement is successfully executed on postgresqk backed (i can see it in pgsql logs), here's my code snippet:

conn = psycopg2.connect("...")
cursor = conn.cursor()

try:
    cursor.callproc("merge_data", ['898989', '111111111', '10000'])
except:
    print "ERROR !"

cursor.close()
like image 393
zfou Avatar asked Jun 08 '12 14:06

zfou


1 Answers

Resolved !

By default, psycopg2 doesnt autocommit on each executed statement, so once i close the connection (at the end) all pending transactions are discarded as if a ROLLBACK was performed.

One may call conn.commit() after each cursor.execute() or .callproc, or just set it at a global scope using

conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
like image 103
zfou Avatar answered Sep 30 '22 13:09

zfou