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.\n");
  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\n");
  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\n");
  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\n");
  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 instances */
 for (i = 0; i < PQntuples(res); i++)
 {
  for (j = 0; j < nFields; j++)
   printf("%-15s", PQgetvalue(res, i, j));
  printf("\n");
 }

 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]# 
复制代码
目录
相关文章
|
机器人
如何查询OpenAI账户余额?ChatGPT怎么查看账户余额的方法
ChatGPT是美国OpenAI研发的聊天机器人程序,也是最近火爆全网的热门应用和话题之王。很多用户在使用openai的时候不知道如何查询OpenAI账户余额?
3470 0
|
网络协议 关系型数据库 PHP
Cloudreve搭建云盘系统,并实现随时访问
Cloudreve搭建云盘系统,并实现随时访问
469 1
Cloudreve搭建云盘系统,并实现随时访问
|
机器学习/深度学习 传感器 大数据
大数据如何化解城市交通拥堵的难题?
大数据如何化解城市交通拥堵的难题?
412 5
|
小程序 JavaScript Java
人事|人事管理系统|基于Springboot的人事管理系统设计与实现(源码+数据库+文档)
人事|人事管理系统|基于Springboot的人事管理系统设计与实现(源码+数据库+文档)
680 1
STM32CubeMX 按键控制LED
STM32CubeMX 按键控制LED
550 0
|
Java 编译器
Java中各种运算符的使用
`long`类型内存8个字节, `int`类型内存4个字节。 `long`取值范围大于`int` ;想要赋值成功,只有通过**强制类型转换**,将 `long` 类型强制转换成`int`类型才能赋值。 - **强制转换**:将 **取值范围大的类型 强制转换成 取值范围小的类型**;比较而言,**自动转换是Java自动执行的,而强制转换需要我们自己手动执行。**
151 0
|
应用服务中间件 Shell nginx
docker学习笔记2,入门到精通
docker学习笔记2,入门到精通
142 0
codeforces 327 A Ciel and Dancing
给你一串只有0和1的数字,然后对某一区间的数翻转1次(0变1 1变0),只翻转一次而且不能不翻转,然后让你计算最多可能出现多少个1。 这里要注意很多细节 比如全为1,要求必须翻转,这时候我们只要翻转一个1就可以了,对于其他情况,我们只要计算区间里面如果0多于1,将其翻转后计算1的总数,然后取最大值。
105 0
|
Java
Java枚举2
Java枚举2
213 0
Java枚举2
|
存储 算法