PQgetf - Gets one or more values from a PGresult in a scanf fashion.
#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);
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 functions 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.
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.
Using PQgetf on a PGresult 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(). 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 doesnt 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);
A contribution of eSilo, LLC. for the PostgreSQL Database Management System. Written by Andrew Chernow and Merlin Moncure.
Report bugs to <[email protected]>.
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.
pqt-specs(3), PQgeterror(3)
Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.
Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.
Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.
Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.
The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.
Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.
Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.
Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.