#include <stdio.h>
#include <libpq-fe.h>
int main() {
PGconn *conn;
PGresult *res;
char *pghost = NULL;
char *pgport =NULL;
char *pgoptions =NULL;
char *pgtty = NULL;
char *dbname =\"test_db\";
/**数据库名*/
int i = 0,t = 0,s,k;
conn = PQsetdb(pghost,pgport,pgoptions,pgtty,dbname);
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr,\"Connection to database '%s' failed!\\n\",dbname);
PQfinish(conn);
eturn 0;
}
res = PQexec(conn,\"SELECT * FROM test\");
/**运行查询命令*/
if( PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,\"Exec Query Fauled!\\n\");
PQclear(res);
return 0;
}
i = PQntuples(res);
/**取得查询的结果的记录的数量*/
t = PQnfields(res);
/**取得字段数量*/
for(s=0; s<i;s++) {
for (k = 0; k<t; k++) {
printf(\"%s\",PQgetvalue(res,s,k));
printf(\" \");
}
printf(\"\\n\");
}
PQfinish(conn);
PQclear(res);
return 0;
}
网管下载dl.bitscn.com
|