Client 3—Simple Processing—PQexec() and PQprint()

Now let's turn our attention to the task of processing a query. I'll start by showing a simple example—you'll connect to a database, execute a hard-wired query, process the results, clean up, and exit.

 1 /* 2 ** File: client3.c 3 */ 4 5 #include <stdlib.h> 6 #include <libpq-fe.h> 7 8 void process_query( PGconn * connection, const char * query_text ) 9 { 10 PGresult * result; 11 PQprintOpt options = {0}; 12 13 if(( result = PQexec( connection, query_text )) == NULL ) 14 { 15 printf( "%s\n", PQerrorMessage( connection )); 16 return; 17 } 18 19 options.header = 1; /* Ask for column headers */ 20 options.align = 1; /* Pad short columns for alignment */ 21 options.fieldSep = "|"; /* Use a pipe as ...

Get PostgreSQL, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.