Updated on 2025-05-29 GMT+08:00

Processing Data in a Result Set

libpq provides functions, such as PQnfields and PQfname, to help you properly parse and process the results of SELECT queries.

The following is an example (for details about the complete example, see Establishing a Database Connection, Executing SQL Statements, and Returning Results):

/* Print out the attribute name. */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
    printf("%-15s", PQfname(res, i));
printf("\n\n");

/* Print lines. */
for (i = 0; i < PQntuples(res); i++)
{
    for (j = 0; j < nFields; j++)
	    printf("%-15s", PQgetvalue(res, i, j));
    printf("\n");
}

/* Release the memory of the result object to avoid memory leakage. */
PQclear(res);