Today I ran into problems when combining a C program with SQL statements
with the ECPG interface of PostgreSQL:
According to the documentation, it should be possible with
EXEC SQL WHENEVER NOT FOUND action
to trigger an action when a query does not return a row.
This does not seem to function. Here is the C program I tested:
----------------------------------------------------------------
/* Test for ecpg interface */
/* An empty table
t (n INTEGER);
must exist in the database
*/
#include <stdio.h>
EXEC SQL BEGIN DECLARE SECTION;
int i;
EXEC SQL END DECLARE SECTION;
int main ()
{ int c;
ECPGdebug(1,stdout);
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL WHENEVER NOT FOUND GOTO notfound;
EXEC SQL CONNECT TO unix:postgresql://localhost/mydb USER rank;
EXEC SQL SELECT n INTO :i FROM t;
printf ("%d\n", i);
EXEC SQL DISCONNECT;
exit (0);
notfound:
printf ("no data found\n");
EXEC SQL DISCONNECT;
exit (0);
}
----------------------------------------------------------------
When running this program on an empty table t in the database, it produces:
[1116]: ECPGdebug: set to 1
[1116]: ECPGconnect: opening database mydb on <DEFAULT> port <DEFAULT>
for user rank
[1116]: ECPGexecute line 16: QUERY: select n from t on connection mydb
[1116]: ECPGexecute line 16: Correctly got 0 tuples with 1 fields
[1116]: raising sqlcode 100 in line 16, 'No data found in line 16.'.
0
[1116]: ecpg_finish: Connection mydb closed.
We can see that sqlcode is set correctly, but the jump to the C-label
notfound does not occur.
Is this a bug in ECPG or am I doing something wrong?
Thanks in advance for your help,
Christian Rank