PQgetf - Gets one or more values from a PGresult in a scanf fashion.
Contents
Copyright
Copyright (c) 2011 eSilo, LLC. All rights reserved.
This is free software; see the source for copying conditions. There is NO warranty; not even for
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description
The PQgetf() and PQgetvf() functions get one or more field values from a PGresult using a scanf style
interface. The tup_num argument indicates which tuple to read values from; values can only be read from
one tuple at a time.
The format argument is a data type specifier string indicating the values to retrieve, like %int4 or
#timestamp. Any number of fields, in any order, can be retrieved in a single call. Each data type
specifier has a cooresponding field_num,type-args,[...] contained within the function´s variable
argument list. The field_num either indicates tuple field number or tuple field name, depending on
whether the data type in format used a ´%´ or ´#´ specifer mark (`man pqt-specs(3)´). The type-args can
be one or more arguments required by the specific data type: for example, "%int4" would require a pointer
to a PGint4. All built-in PostgreSQL data types require a single pointer value.
Examples
UsingPQgetfonaPGresult
The example uses the libpq PQexec function to execute a query and then uses PQgetf() to retrieve field
values. It is important to note that libpqtypes execution functions, like PQparamExec(3), do not need to
generate the PGresult provided to PQgetf().
intsuccess;PGint4i4;PGtexttext;PGbyteabytea;PGpointpt;PGresult*res=PQexec(conn,"SELECTi,t,b,pFROMtbl");/*Getsomefieldvaluesfromtheresult(orderdoesn´tmatter)*/success=PQgetf(res,0,/*getfieldvaluesfromtuple0*/"%int4#text%bytea%point",/*typeformatspecifiers(gettextbyname´#´)*/0,&i4,/*getanint4fromfieldnum0*/"t",&text,/*getatextfromfieldname"t"*/2,&bytea,/*getabyteafromfieldnum2*/3,&pt);/*getapointfromfieldnum3*//*OutputanerrormessageusingPQgeterror(3).*/if(!success)fprintf(stderr,"*ERROR:%s\n",PQgeterror());/*Outputthevalues,dothisbeforePQclear()*/elseprintf("int4=%d,text=%s,bytea=%dbytes,point=(%f,%f)\n",i4,text,bytea.len,pt.x,pt.y);PQclear(res);Name
PQgetf - Gets one or more values from a PGresult in a scanf fashion.
Reporting Bugs
Report bugs to <libpqtypes@esilo.com>.
Return Value
On success, a non-zero value is returned. On error, zero is returned and PQgeterror(3) will contain an
error message.
If the retrieval of any field fails, the get operation is aborted and function will fail. Eventhough
some fields may have succeeded prior to the failure, their values should not be trusted. Instead, make
another PQgetf() call. Getting an array or a composite can lead to memory leaks when getf fails. This
is because these types allocate a PGresult object that must be cleared. To avoid leaks, it is
recommended to perform cleanup even if getf fails, retrieve arrays and composites by themselves or make
them the last field in a getf call.
See Also
pqt-specs(3), PQgeterror(3) libpqtypes 2011 PQgetf(3)
Synopsis
#include<libpqtypes.h>intPQgetf(constPGresult*res,inttup_num,constchar*format,...);intPQgetvf(constPGresult*res,inttup_num,constchar*format,va_listap);
