Database Reference
In-Depth Information
Prepared queries are parsed and planned only once, but nonprepared queries are
parsed and planned every time.
Here is a complete example of how to establish a connection and execute a query
using the connection and execution functions discussed:
/*----------------------------------------------------------------
*
*exec.c
*Executing queries using libpq
*
*IDENTIFICATION
*exec.c
*
*----------------------------------------------------------------
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <limits.h>
#include "libpq-fe.h"
#include "libpq/libpq-fs.h"
char conninfo[] = "hostaddr = '127.0.0.1' port = '5432' dbname =
'testdb'";
Oid paramTypes[] = {23, 25};
int paramLengths[] = {-1, 5};
int paramFormats[] = {0, 0};
int main(int argc, char **argv)
{
PGconn *conn; /* Connection Object */
PGresult *result;
char *paramValues[] = {"2", "bar-2"};
conn = PQconnectdb(conninfo);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "connection to database failed\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
return -1;
}
/* Execute a query using PQexec */
result = PQexec(conn, "CREATE TABLE foo (id INTEGER, name
TEXT)");
 
Search WWH ::




Custom Search