How does one achieve this...
Say I've got a line that reads...
plpy.execute("some sql command that might throw an error");
How do I handle that error?
I've tried:
- Code:
-
create or replace function prepare(tbl varchar, edgetbl varchar) returns integer as $$
plpy.info ("Roads Table: " + tbl)
plpy.info ("Edges Table: " + edgetbl)
createstr = "CREATE TABLE " + edgetbl + " ( gid int8, n_gid int8, dist int8, foreign key (gid) references roads (gid), foreign key (n_gid) references roads (gid), primary key (gid,n_gid) )"
try:
plpy.execute(createstr)
except:
plpy.info ("The table " + edgetbl + " was already created")
plpy.info ("Moving On")
$$ language plpythonu;
Which gave me the output:
INFO: ('Roads Table: roads',)
INFO: ('Edges Table: edges',)
WARNING: plpython: in function prepare:
DETAIL: <class 'plpy.SPIError'>: Unknown error in PLy_spi_execute_query
INFO: ('The table edges was already created',)
INFO: ('Moving On',)
ERROR: relation "edges" already exists
CONTEXT: SQL statement "CREATE TABLE edges ( gid int8, n_gid int8, dist int8, foreign key (gid) references roads (gid), foreign key (n_gid) references roads (gid), primary key (gid,n_gid) )"
However... it does not execute the last line... so the catch is apparently not working.