PQgetf(3)

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

πŸ’– PQgetf(3) πŸ’–

okay bestie, so this is a page from the libpqtypes manual, and it's all about PQgetf β€” the little function that reaches into a PGresult and pulls values out, scanf style. yes. iconic. let's get into it πŸŽ€


✨ NAME ✨

PQgetf β€” Gets one or more values from a PGresult in a scanf fashion.


🌸 SYNOPSIS 🌸

#include <libpqtypes.h>

int PQgetf(const PGresult *res, int tup_num,
           const char *format, ...);
int PQgetvf(const PGresult *res, int tup_num,
            const char *format, va_list ap);

πŸ¦‹ DESCRIPTION πŸ¦‹

So PQgetf() and PQgetvf() grab one or more field values out of a PGresult, and the whole interface is giving scanf, fr fr. The tup_num argument tells it which tuple to read values from β€” and heads up, you can only read values from one tuple at a time, no multitasking here πŸ’….

The format argument is a data type specifier string that says which values you want to retrieve, like %int4 or #timestamp. Any number of fields, in any order, can be retrieved in a single call β€” literally pick and choose, no cap. Each data type specifier has a corresponding field_num, type-args, [...] chillin' in the function's variable argument list. The field_num either means a tuple field number or a tuple field name, depending on whether the data type in format used a % or # specifier mark (man pqt-specs(3)). And the type-args can be one or more arguments that the specific data type requires β€” for example, "%int4" would require a pointer to a PGint4. Every single built-in PostgreSQL data type just needs a single pointer value. simple as that ✨


πŸ’€ RETURN VALUE πŸ’€

On success, a non-zero value comes back. On error, zero comes back, and PQgeterror(3) will be holding an error message for you. πŸ™ˆ

If the retrieval of any field fails, the get operation is aborted and the function will fail. And even though some fields may have succeeded before the failure happened, their values should not be trusted. Instead, make another PQgetf() call. periodt.

Also, getting an array or a composite can lead to memory leaks when getf fails, 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. this is the important part, don't skip it 🫢


πŸŽ€ EXAMPLES πŸŽ€

🌈 Using PQgetf on a PGresult

This example uses the libpq PQexec function to execute a query and then uses PQgetf() to retrieve field values. Important note bestie: libpqtypes execution functions, like PQparamExec(3), do not need to generate the PGresult provided to PQgetf().

int success;
PGint4 i4;
PGtext text;
PGbytea bytea;
PGpoint pt;
PGresult *res = PQexec(conn, "SELECT i,t,b,p FROM tbl");

/* Get some field values from the result (order doesnΒ΄t matter) */
success = PQgetf(res,
     0,            /* get field values from tuple 0 */
     "%int4 #text %bytea %point",
                   /* type format specifiers (get text by name Β΄#Β΄) */
     0,   &i4,     /* get an int4 from field num 0 */
     "t", &text,   /* get a text from field name "t" */
     2,   &bytea,  /* get a bytea from field num 2 */
     3,   &pt);    /* get a point from field num 3 */

/* Output an error message using PQgeterror(3). */
if(!success)
     fprintf(stderr, "*ERROR: %s\n", PQgeterror());

/* Output the values, do this before PQclear() */
else
     printf("int4=%d, text=%s, bytea=%d bytes, point=(%f,%f)\n",
          i4, text, bytea.len, pt.x, pt.y);

PQclear(res);

πŸ‘‘ AUTHOR πŸ‘‘

A contribution of eSilo, LLC. for the PostgreSQL Database Management System. Written by Andrew Chernow and Merlin Moncure. legends only 🫑


πŸ”₯ REPORTING BUGS πŸ”₯

Report bugs to <libpqtypes@esilo.com>.


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.


⭐ SEE ALSO ⭐

pqt-specs(3), PQgeterror(3)


and that's the whole page, bestie 🧚 this was libpqtypes, dated 2011, and the page itself is PQgetf(3). go forth and get those fields, you got this πŸ’–βœ¨πŸ“

πŸ“¦ where this page comes from