473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Query regarding odbc connection ??

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
Jan 10 '07 #1
1 3549
michaelb
534 Expert 512MB
Check your code against the Postgres manual:
http://www.postgresql.org/docs/8.0/static/libpq.html#LIBPQ-CONNECT

This url is for ver.8, you can also look at the main doc entry -
http://www.postgresql.org/docs/manuals/
- and choose a different version. Make a bookmark, you'll need these docs in the future.

In the paragraph Database Connection Control Functions
pay attention to PQsetdbLogin and PQsetdb in the code snippets.

You have dbname, but you may also need to specify host, port, password, etc for your connection to work.

Then, if you want to see whether you can establish a database connection strip off all non-essential code, and use a hard-coded query; something simple like select version() would be a good thing to start with.
Jan 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
4
by: MLH | last post by:
I apologize in advance to forum readers noticing this somewhat redundant post. I fear that my Subject Heading was ill-chosen in earlier post I made on this topic. Am hoping that a clearer Subject...
2
by: Robert | last post by:
when using the following function to create a pass through query is there a way to set the query property, "Returns Rows" to no. The default is yes. Since we are planning to create the pass...
0
by: totierne | last post by:
comp.databases.ms-access, I want to know how to use Oracle views with session variables in Access. The parameterised views in access, are migrated to views with per session variables. The...
11
by: DraguVaso | last post by:
Hi, I want to make a small application in VB.NET that relinks all the query's and tables in an Access database that are linked via ODBC to an SQL Server. It must be able to relink all the tables...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.