next up previous
Next: Conclusion Up: Finer Details Previous: Query

Result

Accessing result information is straightforward. First, only SELECT returns a result set. Other commands like INSERT and DELETE return simple status information.

SELECT's result set is a table made up of rows and columns. Of course, sometimes the result is only one row or one column, but it still needs to be accessed as a table. This accesses the first column in the first row of the result:

 

        printf("%s\n", PQgetvalue(res, 0, 0));
 

The first row and first column are numbered zero. In libpq, PQntuples() returns the number of rows in the result, and PQnfields() returns the number of columns. Using this information, any result value can be retrieved by specifying its row and column. Here is a more complicated example that displays all values returned in a result.

 

        
        int i, j; 
         
        for (i = 0; i < PQntuples(res); i++)                /* loop through all rows returned */
            for (j = 0; j < PQnfields(res); j++)                /* loop through all columns
                printf("%s\n", PQgetvalue(res, i, j));          /* print the value returned */
        
 

Unless a binary cursor is used, all values are returned as ASCII strings. If you need them in a different format for your application, the values must be converted in the application. POSTGRESQL interfaces have many more functions that return useful information:


next up previous
Next: Conclusion Up: Finer Details Previous: Query
Bruce Momjian
2003-01-31