0

I am using C++ and using libpq to access PostgreSQL. I am a newbie to libpq and need to process data of table row by row. below is snippet of code

 PGresult* res = PQexec(conn, "DECLARE myCursor CURSOR FOR select * from my_table");
   if (PQresultStatus(res) != PGRES_COMMAND_OK)
   {
     fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
     PQclear(res);
     exit(1);
   }
   PQclear(res);
   while (true) // need a break condition till data is in cursor
  {
     res = PQexec(conn, "FETCH NEXT in myCursor");
     if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
      //error handling code and break
    }
     if(PQntuples(res) ==0)
        break;
    //handle the query result
   }

Is there a way to check that we have fetched all records and there is no more record left to fetch. Currently I am relying upon

PQresultStatus(res) != PGRES_TUPLES_OK and PQntuples(res) == 0

rveerd
  • 3,620
  • 1
  • 14
  • 30

1 Answers1

0

when PQntuples(res) is 0 or lesser than the number of rows you try to fetch it means no more data to be fetched.