postgresql libpq c接口 操作数据库例子

来源:互联网 发布:ipad看爱情动作片软件 编辑:程序博客网 时间:2024/05/01 10:06

postgresql libpq c接口 操作数据库例子

/* * testlibpq.c * *      Test the C version of libpq, the PostgreSQL frontend library. */#include <stdio.h>#include <stdlib.h>#include <libpq-fe.h>static voidexit_nicely(PGconn *conn){    PQfinish(conn);    exit(1);}intmain(int argc, char **argv){    const char *conninfo;    PGconn     *conn;    PGresult   *res;    int         nFields;    int         i,                j;    /*     * If the user supplies a parameter on the command line, use it as the     * conninfo string; otherwise default to setting dbname=postgres and using     * environment variables or defaults for all other connection parameters.     */    if (argc > 1)        conninfo = argv[1];    else        conninfo = "dbname = postgres";    /* Make a connection to the database */    conn = PQconnectdb(conninfo);    /* Check to see that the backend connection was successfully made */    if (PQstatus(conn) != CONNECTION_OK)    {        fprintf(stderr, "Connection to database failed: %s",                PQerrorMessage(conn));        exit_nicely(conn);    }    /*     * Our test case here involves using a cursor, for which we must be inside     * a transaction block.  We could do the whole thing with a single     * PQexec() of "select * from pg_database", but that's too trivial to make     * a good example.     */    /* Start a transaction block */    res = PQexec(conn, "BEGIN");    if (PQresultStatus(res) != PGRES_COMMAND_OK)    {        fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));        PQclear(res);        exit_nicely(conn);    }    /*     * Should PQclear PGresult whenever it is no longer needed to avoid memory     * leaks     */    PQclear(res);    /*     * Fetch rows from pg_database, the system catalog of databases     */    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);    }    /* first, print out the attribute names */    nFields = PQnfields(res);    for (i = 0; i < nFields; i++)        printf("%-15s", PQfname(res, i));    printf("\n\n");    /* next, print out the rows */    for (i = 0; i < PQntuples(res); i++)    {        for (j = 0; j < nFields; j++)            printf("%-15s", PQgetvalue(res, i, j));        printf("\n");    }    PQclear(res);    /* close the portal ... we don't bother to check for errors ... */    res = PQexec(conn, "CLOSE myportal");    PQclear(res);    /* end the transaction */    res = PQexec(conn, "END");    PQclear(res);    /* close the connection to the database and cleanup */    PQfinish(conn);    return 0;}
/* * testlibpq2.c *      Test of the asynchronous notification interface * * Start this program, then from psql in another window do *   NOTIFY TBL2; * Repeat four times to get this program to exit. * * Or, if you want to get fancy, try this: * populate a database with the following commands * (provided in 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); * * and do this four times: * *   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 voidexit_nicely(PGconn *conn){    PQfinish(conn);    exit(1);}intmain(int argc, char **argv){    const char *conninfo;    PGconn     *conn;    PGresult   *res;    PGnotify   *notify;    int         nnotifies;    /*     * If the user supplies a parameter on the command line, use it as the     * conninfo string; otherwise default to setting dbname=postgres and using     * environment variables or defaults for all other connection parameters.     */    if (argc > 1)        conninfo = argv[1];    else        conninfo = "dbname = postgres";    /* Make a connection to the database */    conn = PQconnectdb(conninfo);    /* Check to see that the backend connection was successfully made */    if (PQstatus(conn) != CONNECTION_OK)    {        fprintf(stderr, "Connection to database failed: %s",                PQerrorMessage(conn));        exit_nicely(conn);    }    /*     * Issue LISTEN command to enable notifications from the rule's NOTIFY.     */    res = PQexec(conn, "LISTEN TBL2");    if (PQresultStatus(res) != PGRES_COMMAND_OK)    {        fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));        PQclear(res);        exit_nicely(conn);    }    /*     * should PQclear PGresult whenever it is no longer needed to avoid memory     * leaks     */    PQclear(res);    /* Quit after four notifies are received. */    nnotifies = 0;    while (nnotifies < 4)    {        /*         * Sleep until something happens on the connection.  We use select(2)         * to wait for input, but you could also use poll() or similar         * facilities.         */        int         sock;        fd_set      input_mask;        sock = PQsocket(conn);        if (sock < 0)            break;              /* shouldn't happen */        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);        }        /* Now check for input */        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");    /* close the connection to the database and cleanup */    PQfinish(conn);    return 0;}
/* * testlibpq3.c *      Test out-of-line parameters and binary I/O. * * Before running this, populate a database with the following commands * (provided in 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'); * * The expected output is: * * 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>/* for ntohl/htonl */#include <netinet/in.h>#include <arpa/inet.h>static voidexit_nicely(PGconn *conn){    PQfinish(conn);    exit(1);}/* * This function prints a query result that is a binary-format fetch from * a table defined as in the comment above.  We split it out because the * main() function uses it twice. */static voidshow_binary_results(PGresult *res){    int         i,                j;    int         i_fnum,                t_fnum,                b_fnum;    /* Use PQfnumber to avoid assumptions about field order in result */    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;        /* Get the field values (we ignore possibility they are null!) */        iptr = PQgetvalue(res, i, i_fnum);        tptr = PQgetvalue(res, i, t_fnum);        bptr = PQgetvalue(res, i, b_fnum);        /*         * The binary representation of INT4 is in network byte order, which         * we'd better coerce to the local byte order.         */        ival = ntohl(*((uint32_t *) iptr));        /*         * The binary representation of TEXT is, well, text, and since libpq         * was nice enough to append a zero byte to it, it'll work just fine         * as a C string.         *         * The binary representation of BYTEA is a bunch of bytes, which could         * include embedded nulls so we have to pay attention to field length.         */        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");    }}intmain(int argc, char **argv){    const char *conninfo;    PGconn     *conn;    PGresult   *res;    const char *paramValues[1];    int         paramLengths[1];    int         paramFormats[1];    uint32_t    binaryIntVal;    /*     * If the user supplies a parameter on the command line, use it as the     * conninfo string; otherwise default to setting dbname=postgres and using     * environment variables or defaults for all other connection parameters.     */    if (argc > 1)        conninfo = argv[1];    else        conninfo = "dbname = postgres";    /* Make a connection to the database */    conn = PQconnectdb(conninfo);    /* Check to see that the backend connection was successfully made */    if (PQstatus(conn) != CONNECTION_OK)    {        fprintf(stderr, "Connection to database failed: %s",                PQerrorMessage(conn));        exit_nicely(conn);    }    /*     * The point of this program is to illustrate use of PQexecParams() with     * out-of-line parameters, as well as binary transmission of data.     *     * This first example transmits the parameters as text, but receives the     * results in binary format.  By using out-of-line parameters we can     * avoid a lot of tedious mucking about with quoting and escaping, even     * though the data is text.  Notice how we don't have to do anything     * special with the quote mark in the parameter value.     */    /* Here is our out-of-line parameter value */    paramValues[0] = "joe's place";    res = PQexecParams(conn,                       "SELECT * FROM test1 WHERE t = $1",                       1,       /* one param */                       NULL,    /* let the backend deduce param type */                       paramValues,                       NULL,    /* don't need param lengths since text */                       NULL,    /* default to all text params */                       1);      /* ask for binary results */    if (PQresultStatus(res) != PGRES_TUPLES_OK)    {        fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));        PQclear(res);        exit_nicely(conn);    }    show_binary_results(res);    PQclear(res);    /*     * In this second example we transmit an integer parameter in binary     * form, and again retrieve the results in binary form.     *     * Although we tell PQexecParams we are letting the backend deduce     * parameter type, we really force the decision by casting the parameter     * symbol in the query text.  This is a good safety measure when sending     * binary parameters.     */    /* Convert integer value "2" to network byte order */    binaryIntVal = htonl((uint32_t) 2);    /* Set up parameter arrays for PQexecParams */    paramValues[0] = (char *) &binaryIntVal;    paramLengths[0] = sizeof(binaryIntVal);    paramFormats[0] = 1;        /* binary */    res = PQexecParams(conn,                       "SELECT * FROM test1 WHERE i = $1::int4",                       1,       /* one param */                       NULL,    /* let the backend deduce param type */                       paramValues,                       paramLengths,                       paramFormats,                       1);      /* ask for binary results */    if (PQresultStatus(res) != PGRES_TUPLES_OK)    {        fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));        PQclear(res);        exit_nicely(conn);    }    show_binary_results(res);    PQclear(res);    /* close the connection to the database and cleanup */    PQfinish(conn);    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 塑料镀铝浸底漆咬底怎么办 标志408钥匙掉了怎么办 房本测绘页丢了怎么办 房本测绘页信息有误怎么办 税务登记证办完没有年检怎么办 建筑施工升降机司机证怎么办 北京建筑施工证怎么办呢 模拟城市5水抽干了怎么办 ip地址错误网络无法接通怎么办 rhino模型太大打开半天怎么办 日本新干线车票丢了怎么办 房间太干燥怎么办又热 薄荷叶子全干了怎么办 水培栀子花叶子蔫了怎么办 薄荷叶叶边干了怎么办 碗莲叶子发黑腐烂怎么办 龟背叶叶子蔫了怎么办 夏天龟背竹蔫了怎么办 春羽叶子长黄斑怎么办 百合竹叶子发黄掉落怎么办 凤尾蕨叶子蔫了怎么办 绿地珊瑚蕨干了怎么办 翠云草叶子蔫了怎么办 珊瑚蕨叶子烂了怎么办 黑骨茶叶子黑斑怎么办 外场主持没有人互动怎么办 企业年报填错了怎么办 手机忘了放哪了怎么办 燃气卡车没气了怎么办 老房子拆了重建怎么办 加了差的汽油怎么办 事业单位未满5年怎么办 小学孩子不会制订学习计划怎么办 忘记提交原创怎么办百度熊掌号 计算机一级报名表填错了怎么办 离婚了不给孩子生活费怎么办 点击爱奇艺系统提示停止运行怎么办 电视机图像颜色变了怎么办 10万签约被拒怎么办 钢琴弹奏中的折指怎么办! 吉他琴头旋钮将琴头扭坏了怎么办?