29.19. »ùÇà ÇÁ·Î±×·¥

ÀÌÇϸ¦ Æ÷ÇÔÇÑ »ùÇà ÇÁ·Î±×·¥ÀÌ, ¼Ò½º ÄÚµå ¹èÆ÷¹°³»ÀÇsrc/test/examplesµð·ºÅ丮¿¡ ÀÖ½À´Ï´Ù.

Example 29-1. libpq »ùÇà ÇÁ·Î±×·¥ 1

/*
 * testlibpq.c
 *
 *              C¾ð¾î PostgreSQL ÇÁ·ÐÆ®¿£µå ¶óÀ̺귯¸® libpqÀÇ ½ÃÇè
 */
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"

static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

int
main(int argc, char **argv)
{
    const char *conninfo;
    PGconn     *conn;
    PGresult   *res;
    int         nFields;
    int         i,
                j;

    /*
     * À¯Àú°¡ Ä¿¸Çµå ¶óÀÎÀ¸·Î ÆÄ¶ó¹ÌÅ͸¦ Á¦°øÇßÀ» °æ¿ì, conninfo ¹®ÀÚ¿­·Î¼­ »ç¿ëÇÕ´Ï´Ù.
     * Á¦°øµÇÁö ¾Ê´Â °æ¿ì´Â µðÆúÆ®·Î dbname=postgres¸¦ »ç¿ëÇÕ´Ï´Ù.
     * ±× ¿ÜÀÇ Á¢¼Ó ÆÄ¶ó¹ÌÅÍ¿¡ ´ëÇØ¼­´Â ȯ°æ º¯¼ö³ª µðÆúÆ®¸¦ »ç¿ëÇÕ´Ï´Ù.
    */
    if (argc > 1)
        conninfo = argv[1];
    else
        conninfo = "dbname = postgres";

    /* µ¥ÀÌÅͺ£À̽º¿ÍÀÇ Á¢¼ÓÀ» È®¸³ÇÕ´Ï´Ù. */
    conn = PQconnectdb(conninfo);
    /* ¹é¿£µå¿ÍÀÇ Á¢¼Ó È®¸³¿¡ ¼º°øÇß´ÂÁö¸¦ È®ÀÎÇÕ´Ï´Ù. */
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s",
                PQerrorMessage(conn));
        exit_nicely(conn);
    }

    /*
     * ÀÌ ½ÃÇè ÄÉÀ̽º¿¡¼­´Â Ä¿¼­¸¦ »ç¿ëÇÕ´Ï´Ù.
     * ±× ¶§¹®¿¡, Æ®·£Àè¼Ç ºí·Ï ³»¿¡¼­ ½ÇÇàÇÒ Çʿ䰡 ÀÖ½À´Ï´Ù.
     * ¸ðµÎ¸¦ ´ÜÀÏÀÇ"select * from pg_database"¶ó°í ÇÏ´Â PQexec()·Î ½Ç½ÃÇÏ´Â °Í
     * µµ °¡´ÉÇÏÁö¸¸, ¿¹·Î¼­´Â ³Ê¹« °£´ÜÇÏ´Ù.
     */

    /* Æ®·£Àè¼Ç ºí·ÏÀ» °³½ÃÇÕ´Ï´Ù.  */
    res = PQexec(conn, "BEGIN");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    /*
     * ºÒÇÊ¿äÇÏ°Ô µÇ¸é, ¸Þ¸ð¸® ¸®Å©¸¦ ¸·±â À§Çؼ­ PGresult¸¦ PQclear ÇØ¾ß ÇÒ.
    * leaks
     */
    PQclear(res);

    /*
     * µ¥ÀÌÅͺ£À̽ºÀÇ ½Ã½ºÅÛ Ä«Å»·Î±× pg_database·ÎºÎÅÍ ÇàÀ» ²¨³½´Ù.
     */
    res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);

    res = PQexec(conn, "FETCH ALL in myportal");
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    /* ¿ì¼±, ¼Ó¼º¸íÀ» Ç¥½ÃÇÕ´Ï´Ù.  */
    nFields = PQnfields(res);
    for (i = 0; i < nFields; i++)
        printf("%-15s", PQfname(res, i));
    printf("\n\n");

    /* ±×¸®°í ÇàÀ» Ç¥½ÃÇÕ´Ï´Ù.  */
   for (i = 0; i < PQntuples(res); i++)
    {
        for (j = 0; j < nFields; j++)
            printf("%-15s", PQgetvalue(res, i, j));
        printf("\n");
    }

    PQclear(res);

    /* Æ÷ÅÐÀ» ´Ý´Â´Ù. ¿©±â¿¡¼­´Â ¿¡·¯ üũ´Â »ý·« ÇÕ´Ï´Ù.¡¦ */
    res = PQexec(conn, "CLOSE myportal");
    PQclear(res);

    /* Æ®·£Àè¼Ç¸¦ Á¾·áÇÕ´Ï´Ù. */
   res = PQexec(conn, "END");
    PQclear(res);

    /* µ¥ÀÌÅͺ£À̽º¿ÍÀÇ Á¢¼ÓÀ» ´Ý°í µÞ󸮸¦ ½Ç½ÃÇÕ´Ï´Ù.  */
    PQfinish(conn);

    return 0;
}

Example 29-2. libpq »ùÇà ÇÁ·Î±×·¥ 2

/*
 * testlibpq2.c
 *              ºñµ¿±â Å뺸 ÀÎÅÍÆäÀ̽ºÀÇ ½ÃÇè
 *
 * ÀÌ ÇÁ·Î±×·¥À» ½ÃÀÛÇØ, ´Ù¸¥ À©µµ¿ì·ÎºÎÅÍ psql¸¦ »ç¿ëÇÕ´Ï´Ù.
 *   NOTIFY TBL2;
 * 4ȸ ¹Ýº¹ÇÏ´Â °÷ÀÇ ÇÁ·Î±×·¥Àº Á¾·áÇÕ´Ï´Ù.
 *
 * Á» ´õ ¿­ÁßÇÏ°í ½Í´Ù¸é, ÀÌÇϸ¦ ½Ç½ÃÇØ Áֽʽÿä.
 * ÀÌÇÏÀÇ Ä¿¸Çµå(src/test/examples/testlibpq2.sql·Î Á¦°ø)·Î µ¥ÀÌÅͺ£À̽º¸¦ »ý¼ºÇÕ´Ï´Ù.
 *
 *   CREATE TABLE TBL1 (i int4);
 *
 *   CREATE TABLE TBL2 (i int4);
 *
 *   CREATE RULE r1 AS ON INSERT TO TBL1 DO
 *     (INSERT INTO TBL2 VALUES (new.i); NOTIFY TBL2);
 *
 * ±×¸®°í ÀÌÇϸ¦ 4ȸ ½ÇÇàÇØ Áֽʽÿä.
 *
 *   INSERT INTO TBL1 VALUES (10);
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include "libpq-fe.h"

static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

int
main(int argc, char **argv)
{
    const char *conninfo;
    PGconn     *conn;
    PGresult   *res;
    PGnotify   *notify;
    int         nnotifies;

    /*
     * À¯Àú°¡ Ä¿¸Çµå ¶óÀÎÀ¸·Î ÆÄ¶ó¹ÌÅ͸¦ Á¦°øÇßÀ» °æ¿ì, conninfo ¹®ÀÚ¿­·Î¼­ »ç¿ëÇÕ´Ï´Ù.
     * Á¦°øµÇÁö ¾Ê´Â °æ¿ì´Â µðÆúÆ®·Î dbname=postgres¸¦ »ç¿ëÇÕ´Ï´Ù.
     * ±× ¿ÜÀÇ Á¢¼Ó ÆÄ¶ó¹ÌÅÍ¿¡ ´ëÇØ¼­´Â ȯ°æ º¯¼ö³ª µðÆúÆ®¸¦ »ç¿ëÇÕ´Ï´Ù.
     */
    if (argc > 1)
        conninfo = argv[1];
    else
        conninfo = "dbname = postgres";
    /* µ¥ÀÌÅͺ£À̽º¿ÍÀÇ Á¢¼ÓÀ» È®¸³ÇÕ´Ï´Ù.  */
   conn = PQconnectdb(conninfo);

    /* ¹é¿£µå Á¢¼Ó È®¸³¿¡ ¼º°øÇß´ÂÁö¸¦ È®ÀÎÇÕ´Ï´Ù. */
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s",
                PQerrorMessage(conn));
        exit_nicely(conn);
    }

    /*
     * LISTEN Ä¿¸Çµå¸¦ ½ÇÇàÇØ, INSERT ·ê¿¡ ÀÇÇÑ Å뺸¸¦ À¯È¿ÇÏ°Ô ÇÕ´Ï´Ù.
    */
    res = PQexec(conn, "LISTEN TBL2");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    /*
     * ºÒÇÊ¿äÇÏ°Ô µÇ¸é, ¸Þ¸ð¸® ¸®Å©¸¦ ¸·±â À§Çؼ­ PGresult¸¦ PQclear ÇØ¾ßÇÕ´Ï´Ù.
     * leaks
     */
    PQclear(res);

    /* 4ȸ Å뺸¸¦ ¹ÞÀ¸¸é Á¾·áÇÕ´Ï´Ù.  */
    nnotifies = 0;
    while (nnotifies < 4)
    {
        /*
         * ±× Á¢¼ÓÀ¸·Î ¹«¾ùÀΰ¡°¡ ÀϾ ¶§±îÁö ´ë±âÇÕ´Ï´Ù. ¿©±â¿¡¼­´Â ÀÔ·Â ±â´Ù¸®´Â 
	 * select(2)¸¦ »ç¿ëÇÕ´Ï´Ù. poll()À̳ª À¯»ç ±â´ÉÀ» »ç¿ëÇÏ´Â Àϵµ °¡´ÉÇÕ´Ï´Ù.
         */
        int         sock;
        fd_set      input_mask;

        sock = PQsocket(conn);

        if (sock < 0)
            break;              /* ¹ß»ýÇØ¼­´Â ¾È µË´Ï´Ù.  */

        FD_ZERO(&input_mask);
        FD_SET(sock, &input_mask);

        if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
        {
            fprintf(stderr, "select() failed: %s\n", strerror(errno));
            exit_nicely(conn);
        }

        /* ¿©±â¼­ ÀÔ·ÂÀ» È®ÀÎÇÕ´Ï´Ù.  */
       PQconsumeInput(conn);
        while ((notify = PQnotifies(conn)) != NULL)
        {
            fprintf(stderr,
                    "ASYNC NOTIFY of '%s' received from backend pid %d\n",
                    notify->relname, notify->be_pid);
            PQfreemem(notify);
            nnotifies++;
        }
    }

    fprintf(stderr, "Done.\n");

    /* µ¥ÀÌÅͺ£À̽º¿ÍÀÇ Á¢¼ÓÀ» ´Ý°í µÞ󸮸¦ ½Ç½ÃÇÕ´Ï´Ù.  */
  PQfinish(conn);

    return 0;
}

Example 29-3. libpq »ùÇà ÇÁ·Î±×·¥ 3

/*
 * testlibpq3.c
 *      Çà ÀÌ¿ÜÀÇ ÆÄ¶ó¹ÌÅÍ¿Í ¹ÙÀ̳ʸ® I/OÀÇ ½ÃÇè.
 *
 * ½ÇÇàÀü¿¡, ÀÌÇÏÀÇ Ä¿¸Çµå(src/test/examples/testlibpq3.sql·Î Á¦°ø)¸¦ »ç¿ëÇØ
 * µ¥ÀÌÅͺ£À̽º¸¦ »ý¼ºÇØ Áֽʽÿä.
 *
 * CREATE TABLE test1 (i int4, t text, b bytea);
 *
 * INSERT INTO test1 values (1, 'joe''s place', '\\000\\001\\002\\003\\004');
 * INSERT INTO test1 values (2, 'ho there', '\\004\\003\\002\\001\\000');
 *
 * ÀÌÇÏÀÇ Ãâ·ÂÀÌ ¿¹»óµË´Ï´Ù.
 *
 * tuple 0: got
 *  i = (4 bytes) 1
 *  t = (11 bytes) 'joe's place'
 *  b = (5 bytes) \000\001\002\003\004
 *
 * tuple 0: got
 *  i = (4 bytes) 2
 *  t = (8 bytes) 'ho there'
 *  b = (5 bytes) \004\003\002\001\000
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "libpq-fe.h"

/* ntohl/htonl¿ë */
#include <netinet/in.h>
#include <arpa/inet.h>


static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

/*
 * ÀÌ ÇÔ¼ö´Â À§ÀÇ ÄÚ¸àÆ®·Î Á¤ÀÇÇÑ Å×ÀÌºí¿¡¼­ ¹ÙÀ̳ʸ® Æ÷¸ËÀ¸·Î fetchÇÕ´Ï´Ù.
 * Äõ¸® °á°ú¸¦ Ç¥½ÃÇÕ´Ï´Ù.
 * main() ÇÔ¼ö°¡ 2¹ø »ç¿ëÇϹǷÎ, °á°ú¸¦ ºÐÇÒÇÕ´Ï´Ù.
 */
static void
show_binary_results(PGresult *res)
{
    int         i,
                j;
    int         i_fnum,
                t_fnum,
                b_fnum;

    /* °á°úÀÇ ÇÊµå ¼ø¼­¿¡ ´ëÇØ °¡Á¤À» ÇÏÁö ¾Ê±â À§ÇØ PQfnumber ¸¦ ÀÌ¿ëÇÕ´Ï´Ù. */
    i_fnum = PQfnumber(res, "i");
    t_fnum = PQfnumber(res, "t");
    b_fnum = PQfnumber(res, "b");

    for (i = 0; i < PQntuples(res); i++)
    {
        char       *iptr;
        char       *tptr;
        char       *bptr;
        int         blen;
        int         ival;

        /* ¿­ÀÇ °ªÀ» Ãëµæ(NULL¸¦ ÇÒ ¼ö ÀÖ´Â ÇÑ ¹«½Ã) */
        iptr = PQgetvalue(res, i, i_fnum);
        tptr = PQgetvalue(res, i, t_fnum);
        bptr = PQgetvalue(res, i, b_fnum);

        /*
         * INT4ÀÇ ¹ÙÀ̳ʸ® Ç¥ÇöÀº ³×Æ®¿öÅ© ¹ÙÀÌÆ® ¼ø¼­¿¡ ÀÇÇÕ´Ï´Ù.
         * µû¶ó¼­, ·ÎÄà ¹ÙÀÌÆ® ¼ø¼­¿¡ ¸ÂÃß´Â °ÍÀÌ ÁÁ½À´Ï´Ù.
         */
        ival = ntohl(*((uint32_t *) iptr));

        /*
         * TEXTÇüÀÇ ¹ÙÀ̳ʸ® Ç¥Çöµµ ÀÌ¿Í °°ÀÌ ÅØ½ºÆ®.
         * ´õ¿í libpq´Â ±× ¸¶Áö¸·¿¡ 0 ¹ÙÀÌÆ®¸¦ ºÎ¿©ÇϹǷÎ,
         * C¾ð¾îÀÇ ¹®ÀÚ¿­·Î¼­ ´Ü¼øÇÏ°Ô Ãë±ÞÇÒ ¼ö°¡ ÀÖ½À´Ï´Ù.
        *
         * BYTEA ÀÇ ¹ÙÀ̳ʸ® Ç¥ÇöÀº ¹ÙÀÌÆ®ÀÇ ¸ðÀÓÀÔ´Ï´Ù.  
         * ³»ÀåµÈ nullÀ» Æ÷ÇÔÇϹǷΠÇÊµå ±æÀÌ¿¡ ÁÖÀ§¸¦ ±â¿ïÀÌÁö ¾ÊÀ¸¸é ¾È µË´Ï´Ù.
         */
        blen = PQgetlength(res, i, b_fnum);

        printf("tuple %d: got\n", i);
        printf(" i = (%d bytes) %d\n",
               PQgetlength(res, i, i_fnum), ival);
        printf(" t = (%d bytes) '%s'\n",
               PQgetlength(res, i, t_fnum), tptr);
        printf(" b = (%d bytes) ", blen);
        for (j = 0; j < blen; j++)
            printf("\\%03o", bptr[j]);
        printf("\n\n");
    }
}

int
main(int argc, char **argv)
{
    const char *conninfo;
    PGconn     *conn;
    PGresult   *res;
    const char *paramValues[1];
    int         paramLengths[1];
    int         paramFormats[1];
    uint32_t    binaryIntVal;

    /*
     * À¯Àú°¡ Ä¿¸Çµå ¶óÀÎÀ¸·Î ÆÄ¶ó¹ÌÅ͸¦ Á¦°øÇßÀ» °æ¿ì, conninfo ¹®ÀÚ¿­·Î¼­ »ç¿ëÇÕ´Ï´Ù.
     * Á¦°øµÇÁö ¾Ê´Â °æ¿ì´Â µðÆúÆ®·Î dbname=postgres¸¦ »ç¿ëÇÕ´Ï´Ù.
     * ±× ¿ÜÀÇ Á¢¼Ó ÆÄ¶ó¹ÌÅÍ¿¡ ´ëÇØ¼­´Â ȯ°æ º¯¼ö³ª µðÆúÆ®¸¦ »ç¿ëÇÕ´Ï´Ù.
     */
    if (argc > 1)
        conninfo = argv[1];
    else
        conninfo = "dbname = postgres";

    /* µ¥ÀÌÅͺ£À̽º¿ÍÀÇ Á¢¼ÓÀ» È®¸³ÇÕ´Ï´Ù. */
    conn = PQconnectdb(conninfo);

    /* ¹é¿£µå Á¢¼Ó È®¸³¿¡ ¼º°øÇß´ÂÁö¸¦ È®ÀÎÇÕ´Ï´Ù. */
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s",
                PQerrorMessage(conn));
        exit_nicely(conn);
    }

    /*
     * ÀÌ ÇÁ·Î±×·¥ÀÇ Æ÷ÀÎÆ®´Â ÇàÀ» ³Ñ´Â ÆÄ¶ó¹ÌÅ͸¦ °¡Áö´Â PQexecParams()ÀÇ »ç¿ë ¹æ¹ý,
     * ¹× µ¥ÀÌÅÍÀÇ ¹ÙÀ̳ʸ® Àü¼ÛÀ» ³ªÅ¸³»´Â °ÍÀÔ´Ï´Ù.
     *
     * ÀÌ ÃÖÃÊÀÇ ¿¹´Â ÆÄ¶ó¹ÌÅ͸¦ ÅØ½ºÆ®·Î¼­ °Ç³×ÁÝ´Ï´Ù.
     * ±×·¯³ª °á°ú´Â ¹ÙÀ̳ʸ® Æ÷¸ËÀ¸·Î ¹Þ½À´Ï´Ù.
     * ÇàÀ» ³Ñ´Â ÆÄ¶ó¹ÌÅ͸¦ »ç¿ëÇÏ´Â °ÍÀ¸·Î, µ¥ÀÌÅͰ¡ ÅØ½ºÆ®¿©µµ ÀοëºÎÈ£³ª
     * À̽ºÄÉÀÌÇÁ ó¸®ÇÏ´Â ¸¹Àº ÀåȲÇÑ ¾²·¹±â¸¦ ¾ø¾Ù ¼ö°¡ ÀÖ½À´Ï´Ù.
     * ÆÄ¶ó¹ÌÅÍ °ª ³»ºÎÀÇ ÀοëºÎȣǥ¿¡ ´ëÇØ¼­ Ư¼öÇÑ Ã³¸®¸¦ ½Ç½ÃÇÒ Çʿ䰡 ¾ø´Â °Í¿¡ ÁÖ¸ñÇϽʽÿä.
     */

    /* ÀÌÇϰ¡ ÇàÀ» ³Ñ´Â ÆÄ¶ó¹ÌÅÍÀÇ °ªÀÔ´Ï´Ù.  */
    paramValues[0] = "joe's place";

    res = PQexecParams(conn,
                       "SELECT * FROM test1 WHERE t = $1",
                       1,           /* ÆÄ¶ó¹ÌÅÍ´Â 1°³.  */
                       NULL,        /* ¹é¿£µå ÆÄ¶ó¹ÌÅÍÀÇ ÇüŸ¦ Ãß·ÐÇÕ´Ï´Ù  */
                       paramValues,
                       NULL,        /* ÅØ½ºÆ®À̱⠶§¹®¿¡, ÆÄ¶ó¹ÌÅÍ ±æÀÌ´Â ºÒÇÊ¿ä.  */
                       NULL,        /* µðÆúÆ®·Î ¸ðµç ÆÄ¶ó¹ÌÅÍ´Â ÅØ½ºÆ®.  */
                       1);          /* ¹ÙÀ̳ʸ® °á°ú¸¦ ¿ä±¸.  */

    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    show_binary_results(res);

    PQclear(res);

    /*
     * 2¹øÂ°ÀÇ ¿¹´Â ¹ÙÀ̳ʸ® Çü½Ä Áß¿¡¼­ Á¤¼ö °ª ÆÄ¶ó¹ÌÅ͸¦ °Ç³×ÁÝ´Ï´Ù.
     * ±×¸®°í ´Ù½Ã ¹ÙÀ̳ʸ® Çü½ÄÀ¸·Î °á°ú¸¦ ¹Þ½À´Ï´Ù.
     *
     * ¹é¿£µå¿¡ ÆÄ¶ó¹ÌÅÍ Å¸ÀÔÀ» Ãß·ÐÇϸé PQexecParams ¿¡°Ô ÀüÇÏÁö¸¸,
     * Äõ¸® ÅØ½ºÆ®¾È¿¡ ÆÄ¶ó¹ÌÅÍ ½Éº¼À» ³Ö´Â °Í¿¡ ÀÇÇØ °­Á¦ÀûÀ¸·Î °áÁ¤ÇÕ´Ï´Ù.
     * À̰ÍÀº ¹ÙÀ̳ʸ® ÆÄ¶ó¹ÌÅÍ¿¡ º¸³¾ ¶§ ¾ÈÀüÇϰí ÁÁÀº Å©±âÀÔ´Ï´Ù.
     */

    /* Á¤¼ö °ª "2" ¸¦ ³×Æ®¿öÅ© ¹ÙÀÌÆ® ¼ø¼­¿¡ º¯È¯ */ 
    binaryIntVal = htonl((uint32_t) 2);

    /* PQexecParams ¿ëÀ¸·Î ÆÄ¶ó¹ÌÅÍ ¹è¿­À» ¼³Á¤ÇÕ´Ï´Ù. */
    paramValues[0] = (char *) &binaryIntVal;
    paramLengths[0] = sizeof(binaryIntVal);
    paramFormats[0] = 1;        /* ¹ÙÀ̳ʸ® */

    res = PQexecParams(conn,
                       "SELECT * FROM test1 WHERE i = $1::int4",
                       1,       /* ÆÄ¶ó¹ÌÅÍ´Â 1°³ */
                       NULL,    /* ¹é¿£µå ÆÄ¶ó¹ÌÅÍÀÇ ÇüŸ¦ Ã߷нÃŲ´Ù.  */
                    paramValues,
                       paramLengths,
                       paramFormats,
                       1);      /* ¹ÙÀ̳ʸ® °á°ú¸¦ ¿ä±¸.  */

    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    show_binary_results(res);

    PQclear(res);

    /* µ¥ÀÌÅͺ£À̽º¿ÍÀÇ Á¢¼ÓÀ» ´Ý°í µÞ󸮸¦ ½Ç½ÃÇÕ´Ï´Ù.  */
    PQfinish(conn);

    return 0;
}