61 lines
1.3 KiB
C
61 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <libpq-fe.h>
|
|
|
|
int pg_connect(char* conninfo, PGconn** conn)
|
|
{
|
|
|
|
*conn = PQconnectdb(conninfo);
|
|
if (PQstatus(*conn) != CONNECTION_OK) {
|
|
fprintf(stderr,
|
|
"ERROR: Connection to database failed: %s",
|
|
PQerrorMessage(*conn));
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* set correctely your values here */
|
|
#define PG_HOST "127.0.0.1"
|
|
//#define PG_USER "postgres"
|
|
//#define PG_DB "postgres"
|
|
//#define PG_PASS "postgres"
|
|
//#define PG_PORT 5432
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
/*declarations*/
|
|
char conninfo[250];
|
|
PGconn *conn = NULL;
|
|
|
|
sprintf(conninfo,
|
|
"user=%s password=%s dbname=%s hostaddr=%s port=%d",
|
|
PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);
|
|
printf("main() conninfo: %s\n",conninfo);
|
|
|
|
if (!pg_connect(conninfo, &conn)) {
|
|
printf("main() go to end\n");
|
|
goto end;
|
|
}else{
|
|
printf("main() go NOT to end\n");
|
|
}
|
|
|
|
/*
|
|
Here fit your staff
|
|
*/
|
|
int isThreadSafe=0;
|
|
printf("main() Started...\n");
|
|
isThreadSafe=PQisthreadsafe();
|
|
if(isThreadSafe==1){
|
|
printf("main() libpq is thread-safe\n");
|
|
}else{
|
|
printf("main() libpq is NOT thread-safe\n");
|
|
}
|
|
printf("main() Done.\n");
|
|
|
|
end:
|
|
PQfinish(conn);
|
|
return 0;
|
|
|
|
}
|