博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
libpq中调用prepared statement:
阅读量:7063 次
发布时间:2019-06-28

本文共 2650 字,大约阅读时间需要 8 分钟。

代码如下:

[root@lex tst]# cat testlibpq.c/* * testlibpq.c *  Test the C version of LIBPQ, the POSTGRES frontend library. */#include 
#include
#include "libpq-fe.h"static voidexit_nicely(PGconn *conn){ PQfinish(conn); exit(EXIT_SUCCESS);}intmain(){ 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]# ./testlibpqcust_id        cust_name      3              Taylor         [root@lex tst]#

转载地址:http://emill.baihongyu.com/

你可能感兴趣的文章
FFmpeg深入分析之零-基础
查看>>
java异常——RuntimeException和User Define Exception
查看>>
Design Pattern: Prototype 模式
查看>>
开源倾情奉献:基于.NET打造IP智能网络视频监控系统(三)命令行工具集
查看>>
[Step By Step]在SAP Business Objects Data Services中使用SQL Transform将数据导入到SAP HANA中(SQL Transform)...
查看>>
有关T-SQL的10个好习惯
查看>>
【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版
查看>>
第 19 章 Class
查看>>
利用WCF的P2P共享剪贴板上的数据
查看>>
二分查找模版
查看>>
快速傅里叶变换(FFT)算法【详解】
查看>>
解决Windows10下80端口被PID为4的System占用的问题
查看>>
多个Tomcat同时运行环境配置 - imsoft.cnblogs
查看>>
opengl 教程(24) shadow mapping (2)
查看>>
RxJava 2.x 使用最佳实践
查看>>
java中关于继承的问题
查看>>
认证服务号可通过模板消息向用户发送重要的服务通知
查看>>
OVS 中的各种网络设备 - 每天5分钟玩转 OpenStack(128)
查看>>
Spring源码学习之:模拟实现BeanFactory,从而说明IOC容器的大致原理
查看>>
iOS - UIActionSheet
查看>>