Hi All
Problem : I am unable to retrieve the data from the table in postgres database using C application which i have written in solaris os.
Query: How to check whether connection is there between postgres database and C application(which we have written for accessing the database) ?
My code is comiling successfully if i run it then its giving o/p as following :
output
-bash-3.00$ ./odbc
Enter a max bw: 20
SELECT query failed.
C code:
#include <stdio.h>
#include <stdlib.h>
# include <libpq-fe.h>
int main()
{
int entered_bw; /* holds max_bw entered by user */
char query_string[256]; /* holds constructed SQL query */
PGconn *conn; /* holds database connection */
PGresult *res; /* holds query result */
int i;
conn = PQconnectdb("dbname=postgres"); /* connect to the database */
if (PQstatus(conn) == CONNECTION_BAD) /* did the database connection fail
? */
{
fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
exit(1);
}
printf("Enter a max bw: "); /* prompt user for a max_bw */
scanf("%2d", entered_bw);
sprintf(query_string, /* create an SQL query string */
"SELECT entered_bw\
FROM ted_3 \
WHERE max_bw = '%d'", entered_bw);
res = PQexec(conn, query_string); /* send the query */
if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
{
fprintf(stderr, "SELECT query failed.\n");
PQclear(res);
PQfinish(conn);
exit(1);
}
for (i = 0; i < PQntuples(res); i++) /* loop through all rows returned */
printf("%s\n", PQgetvalue(res, i, 0)); /* print the value returned */
PQclear(res); /* free result */
PQfinish(conn); /* disconnect from the database */
return 0;
}
My thinking is database (odbc) connection is not there it seems , thats y its givining proper result .
so how to establish odbc connection?
Thanks
Pradeep