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]# 
复制代码
目录
相关文章
|
6月前
|
开发工具 git
解决pre-commit hook failed (add --no-verify to bypass)的问题
该文介绍了两种免去Git预提交钩子(pre-commit)的方法。一是直接进入项目.git/hooks目录,使用`rm -rf ./git/hooks/pre-commit`命令删除pre-commit文件。二是提交时添加`--no-verify`参数,如`git commit --no-verify -m&quot;XXX&quot;`,以跳过预提交检查。
248 0
|
SQL 分布式计算 Spark
marven编译时:<pre>错误: 不允许使用自关闭元素</pre>
marven编译时:<pre>错误: 不允许使用自关闭元素</pre>
97 0
|
自然语言处理 Python
A reportable application error has occurred. Conda has prepared the above report......
A reportable application error has occurred. Conda has prepared the above report......
240 0
A reportable application error has occurred. Conda has prepared the above report......
|
SQL Java 关系型数据库
开发指南—Prepared语句
本文介绍了Prepare协议的概念、用途及在Java中的开启方法。
|
关系型数据库 PostgreSQL 数据库连接