Thread: I have a C to postgres question.

I have a C to postgres question.

From
"Jay G. Scott"
Date:
I'm hoping it's okay to post this kind of thing here.  There's
no newsgroup, cruising the FAQs didn't turn up anything--or
I missed it.

The database has 4 values; I can see them when I get into the d.b.
via:
    psql test
and, using (cut and paste) the string the C program uses to select,
from "psql test" I see the four values.
The string, I suppose, must be okay.   Furthermore, if I turn on
tracing in the C program, I believe I see the four values being printed.
That makes me think the select statement syntax is correct.
However, my C program thinks it got 0 rows.  And I can't retrieve anything.
So I think the problem is in my C code.  I printed the online man
pages, it looks okay to me.
Here's the code fragment:
/*-----------------------------------------------*/
sprintf(select_stmt, "SELECT m_id FROM machines WHERE hostname = '%s'",
node);
fprintf(stderr,"trying this: %s\n", select_stmt);
/*____________________________________________________________________________*/
igot = PQsendQuery(conn_to_db,select_stmt);
fprintf(stderr,"igot from query is %d\n", igot );
seek_m_id = PQgetResult(conn_to_db);

estgot =  PQresultStatus( seek_m_id );

fprintf(stderr,"estgot from PQgetResult is %d\n", estgot );

while( seek_m_id != NULL )
        {
        fprintf(stderr,"seek_m_id != NULL\n");
        seek_m_id = PQgetResult(conn_to_db);
        estgot =  PQresultStatus( seek_m_id );
        tmwh( estgot );
        /* i suppose nrows could keep coming back as 1 */
        nrows = PQntuples( seek_m_id );
        fprintf(stderr,"nrows = %d\n", nrows );
        for(jj=0;jj<nrows;jj++)
                {
                cgot = PQgetvalue( seek_m_id, jj, 0 );
                machine_id = atoi( cgot );
                fprintf(stderr,"machine_id = %d\n", machine_id );
                }
        PQclear(seek_m_id);
        }
/*____________________________________________________________________________*/

--
Jay Scott        512-835-3553        gl@arlut.utexas.edu
Head of Sun Support, Sr. Operating Systems Specialist
Applied Research Labs, Computer Science Div.                   S224
University of Texas at Austin

Re: I have a C to postgres question.

From
Tom Lane
Date:
"Jay G. Scott" <gl@arlut.utexas.edu> writes:
> igot = PQsendQuery(conn_to_db,select_stmt);
> fprintf(stderr,"igot from query is %d\n", igot );
> seek_m_id = PQgetResult(conn_to_db);             <<<<<<<<<<<<<<<<<<<<<<<<

> estgot =  PQresultStatus( seek_m_id );

> fprintf(stderr,"estgot from PQgetResult is %d\n", estgot );

> while( seek_m_id != NULL )
>         {
>         fprintf(stderr,"seek_m_id != NULL\n");
>         seek_m_id = PQgetResult(conn_to_db);   <<<<<<<<<<<<<<<<<<<<<<<<
>         estgot =  PQresultStatus( seek_m_id );
>         tmwh( estgot );
>         /* i suppose nrows could keep coming back as 1 */
>         nrows = PQntuples( seek_m_id );

You did too many PQgetResult's.  I'd suggest coding like

while ((seek_m_id = PQgetResult(conn_to_db)) != NULL)
{
    ... process seek_m_id ...
    PQclear(seek_m_id);
}

so that there's only one PQgetResult call.

            regards, tom lane