• libpq中调用prepared statement:


    代码如下:

    [root@lex tst]# cat testlibpq.c
    /*
     * testlibpq.c
     *  Test the C version of LIBPQ, the POSTGRES frontend library.
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include "libpq-fe.h"
    
    static void
    exit_nicely(PGconn *conn)
    {
     PQfinish(conn);
     exit(EXIT_SUCCESS);
    }
    
    int
    main()
    {
     int   nFields;
     int   i,
           j;
    
    #ifdef DEBUG
     FILE    *debug;
    #endif  /* DEBUG */
    
     PGconn    *conn;
     PGresult   *res;
    
     const char *conninfo="postgresql://postgres:postgres@localhost:5432/postgres";
    
     /* make a connection to the database */
     conn = PQconnectdb(conninfo);
    
     /* check to see that the backend connection was successfully made */
     if (PQstatus(conn) == CONNECTION_BAD)
     {
      fprintf(stderr, "Connection to database failed.
    ");
      fprintf(stderr, "%s", PQerrorMessage(conn));
      exit_nicely(conn);
     }
    
    #ifdef DEBUG
     debug = fopen("/tmp/trace.out", "w");
     PQtrace(conn, debug);
    #endif  /* DEBUG */
    
     /* start a transaction block */
     res = PQexec(conn, "BEGIN");
     if (PQresultStatus(res) != PGRES_COMMAND_OK)
     {
      fprintf(stderr, "BEGIN command failed
    ");
      PQclear(res);
      exit_nicely(conn);
     }
    
     PQclear(res);
    
     ////////////////////////////////////////////////////////////////////////////////////
     const char *stmt_name = "test_stmt";
     const char *stmt = "select * from customers where cust_id=$1";
    
     Oid param_types[1];
     param_types[0] = 0; ///let db to judge it.
    
     res = PQprepare(conn, stmt_name, stmt,1,param_types);
     if (PQresultStatus(res) != PGRES_COMMAND_OK)
     {
      fprintf(stderr, "PQprepare failed
    ");
      PQclear(res);
      exit_nicely(conn);
     }
    
     PQclear(res);
    
     const char* custid = "3";
     const char* param_values[1];
     param_values[0] =custid;
    
     int param_lengths[1];
     param_lengths[0] = 1;
    
     int param_formats[1];
     param_formats[0] = 0;
     
     res = PQexecPrepared(conn, stmt_name, 1, param_values, param_lengths,
                            param_formats, 0);
    
     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     {
      fprintf(stderr, "PQexecPrepared statement didn't return tuples properly
    ");
      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("
    
    ");
    
     /* next, print out the instances */
     for (i = 0; i < PQntuples(res); i++)
     {
      for (j = 0; j < nFields; j++)
       printf("%-15s", PQgetvalue(res, i, j));
      printf("
    ");
     }
    
     PQclear(res);
    
     /* end the transaction */
     res = PQexec(conn, "END");
     PQclear(res);
    
     /* close the connection to the database and cleanup */
     PQfinish(conn);
    
    #ifdef DEBUG
     fclose(debug);
    #endif  /* DEBUG */
    
     return 0;
    }
    [root@lex tst]# 

    编译和运行:

    [root@lex tst]# gcc -c -I/usr/local/pgsql/include testlibpq.c
    [root@lex tst]# gcc -o testlibpq testlibpq.o -L/usr/local/pgsql/lib -lpq
    [root@lex tst]# ./testlibpq
    cust_id        cust_name      
    
    3              Taylor         
    [root@lex tst]# 
  • 相关阅读:
    Leaflet中实现点击播放视频和关闭视频
    Leaflet中添加标注和popup,并且点击时弹窗显示图片
    Leaflet中使用Leaflet.Path.Transform插件实现旋转图形
    Leaflet中绘制同心圆、多个中心对称多边形、平行四边形网格、矩形网格
    Leaflet中使用Leaflet.AnimatedMarker插件实现要素轨迹移动
    Leaflet中使用leaflet.polylineDecorator插件绘制箭头线及虚线矩形
    Leaflet中使用draw绘制时获取图形的几何信息
    Leaflet中使用Leafletecharts3插件实现航班航线动态模拟
    Leaflet中通过setStyle实现图形样式编辑
    Leaflet中实现矩形闪烁动画效果
  • 原文地址:https://www.cnblogs.com/gaojian/p/3140491.html
Copyright © 2020-2023  润新知