PQexecf(3)

πŸ“¦ version 1.5.1, in debian bookworm, ubuntu jammy, ubuntu noble

✨ PQexecf ✨

okay bestie, we're in the libpqtypes manual and today's main character is PQexecf β€” she prepares your params and executes your command, all in one go, no cap πŸ’…


🌸 NAME 🌸

PQexecf β€” Prepares parameters and executes a command.


πŸŽ€ SYNOPSIS πŸŽ€

#include <libpqtypes.h>

PGresult *PQexecf(const PGconn *conn, const char *cmd, ...);
PGresult *PQexecvf(const PGconn *conn, const char *cmd, va_list ap);
int PQsendf(const PGconn *conn, const char *cmd, ...);
int PQsendvf(const PGconn *conn, const char *cmd, va_list ap);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the way that PQexecf() works is: it executes a command that uses libpqtypes type specifiers instead of the whole $1, $2, etc... syntax. the vibe is basically combining PQputvf() and PQparamExec() into a single call. ✨

your variable argument list has to match the type specs listed inside the cmd, fr fr. and the type specifiers go exactly where you'd normally put $1, $2, etc...

PQexecvf() is literally identical to PQexecf() except it takes a va_list. same girl, different entrance.

then we have PQsendf() and PQsendvf() β€” they're identical to PQexecf() and PQexecvf() except they're the asynchronous versions, meaning you have to make an additional call, PQgetResult, to actually get the PGresult object. no skipping steps bestie 😭 for more info, see the PostgreSQL Documentation: specifically the "Asynchronous Command Processing" section under "Client Interfaces | libpq - C library".

also!! a prepared type spec "@name" can be used with these functions, as long as PQspecPrepare() was called with a non-zero value for the is_stmt argument. these functions need an actual SQL statement to execute, periodt.


πŸ‘‘ 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. she will tell you what went wrong, don't worry 🫢

🌈 EXAMPLES 🌈

πŸ’œ Using PQexecf

this example uses the PQexecf function to execute a query.

/* The PQexecf call is shorthand for:
 *
 *   PGparam *param = PQparamCreate(conn);
 *   PQputf(param, "%int4 %int4", 1, 1);
 *   res = PQparamExec(conn, param, "SELECT $1 + $2", 1);
 *   PQparamClear(param);
 *
 * As you may notice, PQexecf makes life much simpler.
 */
PGresult *res = PQexecf(conn, "SELECT %int4 + %int4", 1, 1);

if(!res)
     fprintf(stderr, "PQexecf failed: %s", PQgeterror());
else
     PQclear(res);

/* A bit more common, this puts an int4 and a text into a generated
 * PGparam and then executes 'myfunc($1, $2)'
 */
res = PQexecf(conn, "SELECT * FROM myfunc(%int4, %text)", 2, "abc");

/* Prepared type spec example.  To use with execf, the final "is_stmt"
 * argument must be set to a non-zero value!
 */
PQspecPrepare(conn, "account_insert", "INSERT INTO account VALUES "
     "(%int8, %text, %int4)", 1);

PGint8 acc_id = 78236;
PGtext acc_name = "ABC Coders, LLC.";
PGint4 acc_biz = ACC_BIZ_TECHNOLOGY;

PQexecf(conn, "@account_insert", acc_id, acc_name, acc_biz);

🌸 AUTHOR 🌸

a contribution of eSilo, LLC. for the PostgreSQL Database Management System. written by Andrew Chernow and Merlin Moncure. iconic duo behavior honestly πŸ’…


πŸ™ˆ REPORTING BUGS πŸ™ˆ

report bugs to <libpqtypes@esilo.com>. they wanna hear it, bestie 🫢


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. so like... use her responsibly okay 😭


⭐ SEE ALSO ⭐

PQgeterror(3), PQputvf(3), PQparamExec(3)


libpqtypes Β· 2011 Β· and that's the whole PQexecf(3) page bestie β€” go forth and execute some commands, you absolute legend πŸ’…βœ¨πŸ¦‹πŸŽ€πŸ’–

πŸ“¦ where this page comes from