I'm trying to call a function from psycopg2
like such:
conn = psycopg2.connect(host="name.host.ex", user="username", password="secret")
cur = conn.cursor()
cur.callproc("f_do_action", ["aaa", "bbb"])
cur.close()
conn.close()
When calling this function from psql
everything works fine but using psycopg2
nothing seems to happen. I get no exception. It just does not call out the function on the actual database.
Also other queries from psycopg2
work (SELECT, INSERT
).
Many Python types are supported out-of-the-box and adapted to matching PostgreSQL data types; adaptation can be extended and customized thanks to a flexible objects adaptation system. Psycopg 2 is both Unicode and Python 3 friendly.
In Postgres, the main functional difference between a function and a stored procedure is that a function returns a result, whereas a stored procedure does not. This is because the intention behind a stored procedure is to perform some sort of activity and then finish, which would then return control to the caller.
Try committing before closing your connection:
cur.close()
conn.commit()
conn.close()
From psycopg2 documentation:
Note that closing a connection without committing the changes first will cause any pending change to be discarded as if a ROLLBACK was performed (unless a different isolation level has been selected: see set_isolation_level()).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With