Database Reference
In-Depth Information
Dynamic SQL
ECPG also supports prepared statements. In ECPG, we can prepare the query irst
and then execute it. This can be seen in the following example:
/*----------------------------------------------------------------
*
*ecpg_prep.pgc
*Communicating PostgreSQL Server using ECPG
*
*IDENTIFICATION
*ecpg_prep.pgc
*
*----------------------------------------------------------------
*/
#include<stdio.h>
#include<sys/types.h>
#include<stdio.h>
EXEC SQL BEGIN DECLARE SECTION;
char target[] = "unix:postgresql://localhost:5432/postgres";
const char *stmt = "INSERT INTO foo VALUES(?, ?);";
int id;
char name[255];
EXEC SQL END DECLARE SECTION;
int main(int argc, char **argv)
{
id = 1;
fprintf(name, "%s", "bar");
EXEC SQL CONNECT TO :target AS connection_unix USER postgres;
/* Drop table */
EXEC SQL DROP TABLE IF EXISTS foo;
/* Create table foo */
EXEC SQL CREATE TABLE foo (id INTEGER, name TEXT);
/* Insert values using Prepared statement */
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL PREPARE prepared_stmtFROM :stmt;
/* Using Host variable to insert value */
EXEC SQL EXECUTE prepared_stmtUSING :id, :name;
EXEC SQL COMMIT;
EXEC SQL DISCONNECT ALL;
return 0;
}
 
Search WWH ::




Custom Search