|
/* モジュール言語方式 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include "postgres.h" #include "libpq-fe.h" int main(int argc,char **argv) { char dbName[255] = "dbmaster"; char sql[255]; int i; PGconn *con; PGresult *res; char *kou1,*kou2; con = PQsetdb("","",NULL,NULL,dbName); if ( PQstatus(con) == CONNECTION_BAD ) { fprintf(stderr, "Connection to database '%s' failed.\n", dbName); fprintf(stderr,"%s",PQerrorMessage(con)); exit(1); } |
sprintf(sql,"select * from sample"); res = PQexec(con,sql); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr,"%s",PQerrorMessage(con)); exit(1); } printf(" tel | name\n"); printf("-----+----------------\n"); for(i = 0; i < 2 ;i++) { kou1 = PQgetvalue(res,i,0); kou2 = PQgetvalue(res,i,1); printf("%5s|%s\n",kou1,kou2); } PQclear(res); return 0; } |
ライブラリに用意されている関数
PGsetdb,PQStatus,PQerrorMessage
PQresultStatus,PQgetvalue, ・・・
他社RDBMSでは動かない