Database Reference
In-Depth Information
Using PQexecParams
In the
PQexec
function, there is only one parameter other than the
PGconn
object that
is a command string. So, if we want to send the command where some values are
outside the command string, then we need the
PQexecParams
function. The syntax
for
PQexecParams
is as follows:
PGresult *PQexecParams(PGconn *conn,
const char *command,
int nParams,
const Oid *paramTypes,
const char * const *paramValues,
const int *paramLengths,
const int *paramFormats,
insert resultFormat);
This function takes extra arguments that can be used to supply the extra parameter
outside the command. They are as follows:
•
nParams
: This shows the total number of parameters
•
paramTypes
: This shows the type of parameters
•
paramValue
: This shows the parameter's value array
•
paramLengths
: This shows the length of the parameter array
•
paramFormats
: This shows that the parameters are binary (1) or text (0)
Executing prepared statements
PostgreSQL supports prepared statements. A prepared statement is used to
eficiently execute the same or similar query repeatedly. First, the query is prepared
in the form of a template using
placeholder
and values are substituted during
execution. In PostgreSQL, the
$
sign is used to identify the placeholder.
Using PQprepare
The
PQprepare
function submits a request to create a prepared statement with the
given parameters and waits for completion. The syntax for
PQprepare
is as follows:
PGresult *PQprepare(PGconn *conn,
const char *stmtName,
const char *query,
int nParams,
const Oid *paramTypes);