libpq程序例子
程序:
[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()
{
char *pghost,
*pgport,
*pgoptions,
*pgtty;
char *dbName;
int nFields;
int i,
j; #ifdef DEBUG
FILE *debug;
#endif /* DEBUG */ PGconn *conn;
PGresult *res; /*
* Begin by setting the parameters for a backend connection.
* If the parameters are NULL, the system tries to use
* reasonable defaults by looking up environment variables
* or, failing that, using hardwired constants.
*/ const char *conninfo="postgresql://postgres:postgres@localhost:5432/postgres"; /* make a connection to the database */
//conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
conn = PQconnectdb(conninfo); /* check to see that the backend connection was successfully made */
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
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);
} /*
* should PQclear PGresult whenever it is no longer needed
* so as to avoid memory leaks
*/
PQclear(res); /*
* fetch instances from the pg_database, the system catalog of
* databases
*/
res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res); res = PQexec(conn, "FETCH ALL in myportal");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command 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); /* close the portal */
res = PQexec(conn, "CLOSE myportal");
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]#
编译和运行:
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
[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
datname datdba encoding datcollate datctype datistemplate datallowconn datconnlimit datlastsysoid datfrozenxid dattablespace datacl template1 en_US.UTF- en_US.UTF- t t - {=c/postgres,postgres=CTc/postgres}
template0 en_US.UTF- en_US.UTF- t f - {=c/postgres,postgres=CTc/postgres}
postgres en_US.UTF- en_US.UTF- f t -
[root@lex tst]#
libpq程序例子的更多相关文章
- 微信小程序例子-保存图片到手机相册
微信小程序例子-保存图片到手机相册 1.关键代码 1)WXML文件 2)JS文件 saveImgToPhotosAlbumTap: function(){ // 图片必须是 https 的 var I ...
- Peer模式的多线程程序例子
Peer模式的多线程程序例子 程序的模型大概是这样的.有一个master(),用来分发任务.有N个多线程的slave用来处理任务. 主程序里可以这样调用: 可以看出,上面这段程序还是依赖于Proces ...
- 第15.35节 PyQt编程实战:结合QDial实现的QStackedWidget堆叠窗口程序例子
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.案例说明 本案例是老猿在学习QStackedWidget中的一个测试案例,该案例使用QStack ...
- IDEA下调试和运行Hadoop程序例子
准备 配置好JDK和Hadoop环境, 在IDEA中建立maven项目,建立后的目录结构为: 修改pom..xml引入相关支持: <?xml version="1.0" en ...
- 一个简单的Java程序例子以及其几种注释
在说道主题前,先来啰嗦两句,o()︿︶)o 唉,不说两句心里就有个疙瘩,也许这就是所谓的强迫症吧,好了说说我想啰嗦的,其实也就是这样子的,关于Java开发工具箱的下载以及环境的配置.Java开发工具箱 ...
- Spark记录-Scala程序例子(函数/List/match/option/泛型/隐式转换)
object func { def main(args:Array[String]):Unit={ //函数赋值给变量时, 必须在函数后面加上空格和下划线. def sayHello(name: St ...
- ICE简介及C++程序例子(转)
一.ICE简介: 1.ICE是什么? ICE是ZEROC的开源通信协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通信引擎,是一个面向对象 ...
- gevent协程、select IO多路复用、socketserver模块 改造多用户FTP程序例子
原多线程版FTP程序:http://www.cnblogs.com/linzetong/p/8290378.html 只需要在原来的代码基础上稍作修改: 一.gevent协程版本 1. 导入geven ...
- 几个JQuery解析XML的程序例子
用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...
随机推荐
- Android之AndroidManifest.xml文件解析
转自:Android学习笔记之AndroidManifest.xml文件解析 一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文 ...
- API HOOK技术
API HOOK技术是一种用于改变API执行结果的技术,Microsoft 自身也在Windows操作系统里面使用了这个技术,如Windows兼容模式等. API HOOK 技术并不是计算机病毒专有技 ...
- Task-based Asynchronous Pattern (TAP)
The Task-based Asynchronous Pattern (TAP) is based on the System.Threading.Tasks.Task and System.Thr ...
- struts2启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1
好久没有搞struts2,今天配置strut2.2.1,启动时遇到个小问题.记录下. tomcat启动报错: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- php 生成类的对象 $a=new test();
程序 <?php class test { ... } $a=new test(); 1.BNF 范式 start: variable '=' expr ';' expr: new_expr ; ...
- javascript 库
http://overapi.com/javascript/ 查javascript http://slimerjs.org/ 自动测试? http://www.cnblogs.com/lhb25/p ...
- oralce索引和分区索引的使用
oracle分区表和分区索引的本质就是将数据分段存储,包括表和索引(索引从本质上来讲也是表),表分区会将表分成多个段分别存储.由此数据查询过程改变为先根据查询条件定位分区表,然后从该表中查询数据,从而 ...
- 【转】10分钟搭建NDK的Android开发环境
原文网址:http://blog.csdn.net/u012176591/article/details/23018913 作者:金良(golden1314521@gmail.com) csdn博客: ...
- C# 中的枚举类型 enum (属于值类型)
原文 C# 中的枚举类型 enum (属于值类型) C# 支持两种特殊的值类型:枚举和结构. 声明枚举:声明时要声明所有可能的值. using System; using System.Collect ...
- 《深入Java虚拟机学习笔记》- 第13章 逻辑运算
<深入Java虚拟机学习笔记>- 第13章 浮点运算