I'm trying to use Python to run a PostgreSQL function that inserts data from one table into another. If I run the function in pgAdmin it works, but if I run it through Python I get the return value, which says it's executed, but it doesn't insert any records.
Here is my Python code
import psycopg2
from parse_config import fn_parse_config
def run_function():
conn = None
try:
# read database configuration
login_params = fn_parse_config("etl_process.ini", "psycopg_login")
# connect to the PostgreSQL database
conn = psycopg2.connect(**login_params)
# create a cursor object for execution
cur = conn.cursor()
# another way to call a stored procedure
#cur.execute("SELECT * FROM usp_test();")
cur.callproc('usp_test')
# process the result set
row = cur.fetchone()
while row is not None:
print(row)
row = cur.fetchone()
# close the communication with the PostgreSQL database server
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
if __name__ == '__main__':
run_function()
Here is my function
CREATE OR REPLACE FUNCTION usp_test()
RETURNS integer AS $$
BEGIN
INSERT INTO staging_raw_file (Site,
StyleCode)
select CAST(a.Site as VARCHAR(10)),
CAST(a.StyleCode as VARCHAR(30))
from import_raw_file a;
return 5;
END; $$
LANGUAGE plpgsql;