Re: I have a C to postgres question. - Mailing list pgsql-novice

From Tom Lane
Subject Re: I have a C to postgres question.
Date
Msg-id 20158.1043946142@sss.pgh.pa.us
Whole thread Raw
In response to I have a C to postgres question.  ("Jay G. Scott" <gl@arlut.utexas.edu>)
List pgsql-novice
"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

pgsql-novice by date:

Previous
From: Andres Ledesma
Date:
Subject: postmaster don't start ...
Next
From: Josh Berkus
Date:
Subject: Re: Good books?