代码如下:

[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[];
param_types[] = ; ///let db to judge it. res = PQprepare(conn, stmt_name, stmt,,param_types);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "PQprepare failed\n");
PQclear(res);
exit_nicely(conn);
} PQclear(res); const char* custid = "";
const char* param_values[];
param_values[] =custid; int param_lengths[];
param_lengths[] = ; int param_formats[];
param_formats[] = ; res = PQexecPrepared(conn, stmt_name, , param_values, param_lengths,
param_formats, ); 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 = ; i < nFields; i++)
printf("%-15s", PQfname(res, i)); printf("\n\n"); /* next, print out the instances */
for (i = ; i < PQntuples(res); i++)
{
for (j = ; 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 ;
}
[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 Taylor
[root@lex tst]#

libpq中调用prepared statement:的更多相关文章

  1. Mysql中的Prepared Statement与Stored Precedure学习

    可以参考: http://stackoverflow.com/questions/196652/prepared-statement-vs-stored-procedure They are not ...

  2. Java向PostgreSQL发送prepared statement 与 libpq 向PostgreSQL发送prepared statement之比较:

    Java 代码,在数据库端,并没有当成 prepared statetment 被处理. C代码通过libpq 访问数据库端,被当成了 prepared statement 处理.也许是因Postgr ...

  3. Postgre cannot insert multiple commands into a prepared statement

    悲剧... FireDAC连接Postgre数据库, 使用默认的属性, 一次执行多条SQL的时候, 会报"cannot insert multiple commands into a pre ...

  4. 对PostgreSQL的prepared statement的深入理解

    看官方文档: http://www.postgresql.org/docs/current/static/sql-prepare.html PREPARE creates a prepared sta ...

  5. close()方法应该在finally语句中调用吗?

    翻译人员: 铁锚 翻译时间: 2013年12月20日 原文链接: Should .close() be put in finally block or not? 下面列出了关闭输出流(output w ...

  6. Generate PDF in Sourcing through concurrent request,在EBS java并发中调用指定am的方法

    package oracle.apps.pon.printing.cp; import java.io.InputStream; import java.io.FileOutputStream; im ...

  7. MSScriptControl详解(可实现在C#等语言中调用JAVASCRIPT代码)

    ScriptControl接口 属性名称 类型 备注 AllowUI BOOL 检测是否允许运行用户的接口元素.如果为False,则诸如消息框之类的界面元素不可见. CodeObject Object ...

  8. PDO 使用prepared statement 预处理LIMIT等非表字段参数

    由于一些驱动不支持原生的预处理语句,因此PDO可以完全模拟预处理.PDO的模拟预处理是默认打开的,即便MYSQL驱动本身支持预处理,在默认打开的状态下,PDO是不会用到MYSQL本身提供的预处理功能. ...

  9. mysql 执行存储过程报错Prepared statement needs to be re-prepared

    今日思语:不喜欢再见 说再见,因为有时明知道下一次再见已是遥遥无期或是不再见 错误如下: ERROR 1615 (HY000) at line 406 in file: 'update-mysql.s ...

随机推荐

  1. Compass被墙后如何安装安装

    今天安装 Compass 多时候发现竟然安装不了,且什么提示也没有,让人纳闷.安装代码如下: $ gem install compass 运行该段代码后发现没反应,也没有提示,后来网上查了才知道,竟然 ...

  2. [原]Unity3D深入浅出 - 认识开发环境中的Component(组件)菜单

    Component(组件)是用来添加到GameObject对象上的一组相关属性,本质上每个组件都是一个类的实例,比如在Cube上添加一个Mesh网格,即面向对象的思维方式可以理解成Cube对象里包含了 ...

  3. 【转】JNI和NDK的区别

    原文网址:http://blog.csdn.net/ithomer/article/details/6828830 NDK(Native Development Kit)“原生”也就是二进制 andr ...

  4. OpenGL学习之路(五)

    1 引子 不知不觉我们已经进入到读书笔记(五)了,我们先对前四次读书笔记做一个总结.前四次读书笔记主要是学习了如何使用OpenGL来绘制几何图形(包括二维几何体和三维几何体),并学习了平移.旋转.缩放 ...

  5. HDU 5289 Assignment

    题意:求一段长度为n的序列里有多少个子区间内的最大值减最小值小于k. 解法:RMQ+尺取法或单调队列.RMQ可以用st或者线段树,尺取法以前貌似YY出来过……只是不知道是这个东西…… 设两个标记l和r ...

  6. Could not locate executable null 解决办法

    问题导读: 1.建一个MapReduce Project,运行时发现出问题:Could not locate executable null,该如何解决?2.Could not locate exec ...

  7. 扩展类加载器-------改变JAVA的父优先类加载顺序

    java的类加载机制默认情况下是采用委托模型:当加载某个类时JVM会首先尝试用当前类加载器的父类加载器加载该类,若父类加载器加载不到再由当前类加载器来加载,因此这种模型又叫做“父优先”模型. 但是在实 ...

  8. android布局ui

    LinearLayout和RelativeLayout 属性对比 共有属性:java代码中通过btn1关联次控件android:id=”@+id/btn1″ 控件宽度android:layout_wi ...

  9. POJ 3630- Phone List(Trie)

    题意:给一组串,是否存在一个串是另一个的前缀. 分析:val[N]保存树节点上是否组成一个串 #include <map> #include <set> #include &l ...

  10. 十款PHP开发框架对比

    PHP开发框架近来在PHP社区中成为讨论的热点,几乎每天都在推出新的框架.面对市面上超过四十种的开发框架,你很难判断究竟哪一款最适合你,尤其是在这些框架所提供的功能不尽相同的时候.    本文将引导你 ...