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