PostgresSQL server connectiong problem with C-language...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhu542
    New Member
    • Oct 2007
    • 17

    #1

    PostgresSQL server connectiong problem with C-language...

    I wrote C-program for connecting to postgresSQL server and get the data from the database.

    The program is as fallows:-
    [code=c]
    #include <stdio.h>
    #include <stdlib.h>
    #include "libpq-fe.h" /* libpq header file */
    int
    main()
    {
    char state_code[3];
    char query_string[256]; /* holds constructed SQL query */
    PGconn *conn; /* holds database connection */
    PGresult *res;
    int i;
    conn = PQconnectdb("db name=test"); /* 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 state code: "); /* prompt user for a state code */
    scanf("%2s", state_code);
    sprintf(query_s tring, /* create an SQL query string */
    "SELECT name \
    FROM statename \
    WHERE code = ’%s’", state_code);
    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;
    }
    [/code]

    every time I a getting the same error:-

    Connection to database failed.
    could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

    could any plz help me how to solve this problem...

    Thanks & Regards...
    Last edited by weaknessforcats; Oct 12 '07, 04:39 PM. Reason: Added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Please use code tags.

    I added them for you this time.

    Comment

    • madhu542
      New Member
      • Oct 2007
      • 17

      #3
      Originally posted by weaknessforcats
      Please use code tags.

      I added them for you this time.

      I tried with ur code ... but I am still getting the same error...

      even I changed the posgresql.conf and pg_hda.conf. and tried same result.

      Thanks & Regards...

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by madhu542
        I tried with ur code ... but I am still getting the same error...

        even I changed the posgresql.conf and pg_hda.conf. and tried same result.

        Thanks & Regards...
        Moderator has not given new code. He has just put code tags which will make your code and post clearly readable. In Reply Guidelines, it is mentined that when you post any code "Use CODE tags around your code:[ CODE ]..code goes here..[ /CODE ]" So your code is not changed anyway by him :)

        Regards

        Comment

        Working...