PQexecf - Prepares parameters and executes a command.
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 PQexecf() function executes a command that uses libpqtypes type specifiers instead of $1, $2, etc...
syntax. The idea is to combine PQputvf() and PQparamExec() into a single call. The variable argument
list must match the type specs listed within the cmd. The type specifiers should be placed where one
would normally place $1, $2, etc...
The PQexecvf() function is identical to PQexecf() except it takes a va_list.
The PQsendf() and PQsendvf() functions are identical to PQexecf() and PQexecvf() except they are
asynchronous versions, meaning an additional call, PQgetResult, must be issued to get the PGresult
object. For more information, see the PostgreSQL Documentation: specifically the "Asynchronous Command
Processing" section under "Client Interfaces | libpq - C library".
A prepared type spec "@name" can be used with these functions, granted that PQspecPrepare() was called
with a non-zero value for the is_stmt argument. These functions needs an actual SQL statement to
execute.
Examples
UsingPQexecf
The example uses PQexecf function to execute a query.
/*ThePQexecfcallisshorthandfor:**PGparam*param=PQparamCreate(conn);*PQputf(param,"%int4%int4",1,1);*res=PQparamExec(conn,param,"SELECT$1+$2",1);*PQparamClear(param);**Asyoumaynotice,PQexecfmakeslifemuchsimpler.*/PGresult*res=PQexecf(conn,"SELECT%int4+%int4",1,1);if(!res)fprintf(stderr,"PQexecffailed:%s",PQgeterror());elsePQclear(res);/*Abitmorecommon,thisputsanint4andatextintoagenerated*PGparamandthenexecutes'myfunc($1,$2)'*/res=PQexecf(conn,"SELECT*FROMmyfunc(%int4,%text)",2,"abc");/*Preparedtypespecexample.Tousewithexecf,thefinal"is_stmt"*argumentmustbesettoanon-zerovalue!*/PQspecPrepare(conn,"account_insert","INSERTINTOaccountVALUES""(%int8,%text,%int4)",1);PGint8acc_id=78236;PGtextacc_name="ABCCoders,LLC.";PGint4acc_biz=ACC_BIZ_TECHNOLOGY;PQexecf(conn,"@account_insert",acc_id,acc_name,acc_biz);Name
PQexecf - Prepares parameters and executes a command.
Reporting Bugs
Report bugs to <libpqtypes@esilo.com>.
Return Value
PQexecf and PQexecvf return NULL on error and a valid PGresult on success. PQsendf and PQsendvf return
zero on error and a non-zero value on success. On error, use PQgeterror(3) to obtain an error message.
See Also
PQgeterror(3), PQputvf(3), PQparamExec(3) libpqtypes 2011 PQexecf(3)
Synopsis
#include<libpqtypes.h>PGresult*PQexecf(constPGconn*conn,constchar*cmd,...);PGresult*PQexecvf(constPGconn*conn,constchar*cmd,va_listap);intPQsendf(constPGconn*conn,constchar*cmd,...);intPQsendvf(constPGconn*conn,constchar*cmd,va_listap);
