Database Reference
In-Depth Information
Here is the complete example to drop a table, create a table, insert/update/delete
data, and use the cursor to fetch the data. The syntax of SQL commands is the same
as we have discussed in previous chapters in detail.
/*----------------------------------------------------------------
*
*ecpg_prog.pgc
*Connecting PostgreSQL Server using ecpg
*
*IDENTIFICATION
*ecpg_prog.pgc
*
*----------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
EXEC SQL BEGIN DECLARE SECTION;
char target[] = "unix:postgresql://localhost:5432/postgres";
int id;
char name[255];
EXEC SQL END DECLARE SECTION;
int main(int argc, char **argv)
{
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 data into table foo */
EXEC SQL INSERT INTO foo VALUES (1, 'foo1');
EXEC SQL INSERT INTO foo VALUES (2, 'foo2');
EXEC SQL INSERT INTO foo VALUES (3, 'foo3');
/* Selecting data from table foo */
EXEC SQL SELECT id, name FROM foo LIMIT 1;
/* Selecting data from table using cursor */
EXEC SQL DECLARE cur CURSOR FOR SELECT id, name FROM foo WHERE
id = 3;
EXEC SQL OPEN cur;
EXEC SQL FETCH cur INTO :id, :name;
EXEC SQL CLOSE cur;
EXEC SQL COMMIT;
 
Search WWH ::




Custom Search