Query Execution Functions

Once a connection to a database server has been successfully established, the functions described here are used to perform SQL queries and commands.

Main Routines

PQexec

PQexec submits a query to the Red Hat Database backend server and waits for the result.
PGresult *PQexec(PGconn *conn, const char *query);
returns a PGresult pointer or possibly a NULL pointer. A non-NULL pointer will generally be returned except in the case of out-of-memory conditions or other serious errors such as inability to send the query to the backend. If a NULL is returned, it should be treated like a PGRES_FATAL_ERROR result. Use PQerrorMessage to get more information about the error.

The PGresult structure encapsulates the query result returned by the backend.

Note

libpq application programmers should be careful to maintain the PGresult abstraction. Use the accessor functions below to get at the contents of PGresult. Avoid directly referencing the fields of the PGresult structure because they are subject to change in the future. (The definition of struct PGresult is not even provided in libpq-fe.h. If you have old code that accesses PGresult fields directly, you can keep using it by including libpq-int.h as well, but you should fix the code.)

Escaping Strings for Inclusion in SQL Queries

PQescapeString Escapes a string for use within an SQL query.
size_t PQescapeString (char *to, const char *from, size_t length);
If you want to include strings that have been receivedfrom a source that is not trustworthy (for example, because a random userentered them), you cannot directly include them in SQLqueries for security reasons. Instead, you have to quote specialcharacters that are otherwise interpreted by the SQL parser.

PQescapeString performs this operation. Thefrom points to the first character of the string thatis to be escaped, and the length parameter counts thenumber of characters in this string (a terminating zero byte isneither necessary nor counted). to shall point to abuffer that is able to hold at least one more character than twicethe value of length, otherwise the behavior isundefined. A call to PQescapeString writes an escapedversion of the from string to the tobuffer, replacing special characters so that they cannot cause anyharm, and adding a terminating zero byte. The single quotes thatmust surround PostgreSQL string literals are not part of the resultstring.

PQescapeString returns the number of characters writtento to, not including the terminating zero byte.Behavior is undefined when the to and fromstrings overlap.

Escaping Binary Strings for Inclusion in SQL Queries

PQescapeBytea Escapes a binary string (bytea type) for use within an SQL query.
unsigned char *PQescapeBytea(unsigned char *from, size_t from_length, size_t *to_length);   
Certain ASCII characters must be escaped (but all characters may be escaped) when used as part of a bytea string literal in an SQL statement. In general, to escape a character, it is converted into the three digit octal number equal to the decimal ASCII value, and preceded by two backslashes. The single quote (') and backslash (\) characters have special alternate escape sequences. See the Red Hat Database Administrator and User's Guide for more information. PQescapeBytea performs this operation, escaping only the minimally required characters.

The from parameter points to the first character of the string that is to be escaped, and the from_length parameter reflects the number of characters in this binary string (a terminating zero byte is neither necessary nor counted). The to_length parameter shall point to a buffer suitable to hold the resultant escaped string length. The result string length does not include the terminating zero byte of the result.

PQescapeBytea returns an escaped version of the from parameter binary string, to a caller-provided buffer. The return string has all special characters replaced so that they can be properly processed by the PostgreSQL string literal parser, and the bytea input function. A terminating zero byte is also added. The single quotes that must surround PostgreSQL string literals are not part of the result string.

Retrieving SELECT Result Information

Retrieving SELECT Result Values

PQprint

PQprint prints out all the tuples and, optionally, the attribute names to the specified output stream.

void PQprint(FILE* fout,      /* output stream */
   const PGresult *res,
   const PQprintOpt *po);

struct {
    pqbool  header;            /* print output field headings and row count */
    pqbool  align;               /* fill align the fields */
    pqbool  standard;        /* old brain dead format */
    pqbool  html3;             /* output html tables */
    pqbool  expanded;      /* expand tables */
    pqbool  pager;             /* use pager for output if needed */
    char    *fieldSep;         /* field separator */
    char    *tableOpt;        /* insert to HTML table ... */
    char    *caption;          /* HTML caption */
    char    **fieldName;    /* NULL terminated array of replacement field names */
} PQprintOpt;

Retrieving Non-SELECT Result Information