2013/11/07
#includeint main(){ int i; EXEC SQL CONNECT TO webmaster; EXEC SQL BEGIN DECLARE SECTION; int tel=0; char name[20]=""; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE sample_Cursor CURSOR FOR SELECT * FROM sample; EXEC SQL OPEN sample_Cursor; printf("SQLSTATE| tel | name\n"); printf("--------+-----+-------\n"); for(i = 0; i < 3 ;i++) { EXEC SQL FETCH sample_Cursor INTO :tel,:name; printf("%8s|%5d|%s\n",sqlca.sqlstate,tel,name); } EXEC SQL CLOSE sample_Cursor; EXEC SQL DISCONNECT webmaster; return 0; }
## Embedded SQL sample All : sample1 ./sample1 sample1 : sample1.c gcc -o sample1 sample1.c -lecpg sample1.c : sample1.pgc ecpg sample1.pgc clean : rm sample1 sample1.cmakeコマンドでコンパイル、実行する。
/* Processed by ecpg (4.5.0) */ /* These include files are added by the preprocessor */ #include#include #include /* End of automatic include section */ #line 1 "sample1.pgc" #include int main(){ int i; { ECPGconnect(__LINE__, 0, "webmaster" , NULL, NULL , NULL, 0); } #line 4 "sample1.pgc" /* exec sql begin declare section */ #line 6 "sample1.pgc" int tel = 0 ; #line 7 "sample1.pgc" char name [ 20 ] = "" ; /* exec sql end declare section */ #line 8 "sample1.pgc" /* declare sample_Cursor cursor for select * from sample */ #line 11 "sample1.pgc" { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare sample_Cursor cursor for select * from sample", ECPGt_EOIT, ECPGt_EORT);} #line 13 "sample1.pgc" printf("SQLSTATE| tel | name\n"); printf("--------+-----+-------\n"); for(i = 0; i < 3 ;i++) { { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch sample_Cursor", ECPGt_EOIT, ECPGt_int,&(tel),(long)1,(long)1,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_char,(name),(long)20,(long)1,(20)*sizeof(char), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);} #line 19 "sample1.pgc" printf("%8s|%5d|%s\n",sqlca.sqlstate,tel,name); } /* EXEC SQL FETCH sample_Cursor INTO :tel,:name; printf("%8s|%5d|%s\n",sqlca.sqlstate,tel,name); EXEC SQL FETCH sample_Cursor INTO :tel,:name; printf("%8s|%5d|%s\n",sqlca.sqlstate,tel,name); */ { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close sample_Cursor", ECPGt_EOIT, ECPGt_EORT);} #line 29 "sample1.pgc" { ECPGdisconnect(__LINE__, "webmaster");} #line 31 "sample1.pgc" return 0; }