Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1689591
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类: LINUX

2011-04-14 14:33:41

 Linux下libpq开发 在linux下C/C++连接远程postgresql数据库详细步骤

1、下载postgres数据库8.3.1版本,下载网站:

2、在linux下解压缩生成postgresql-8.3.1文件夹

3、参考postgres-8.3.1文件夹中的INSTALL安装数据库,若是linux系统中自带的postgres数据库,安装时可以参考    http://blog.csdn.net/zst126/archive/2007/10/31/1859608.aspx

4、配置完成后远程客户端就可以连接该数据库服务器了。

5、从数据库服务器postgres的lib和include的目录中拷贝以下库和头文件libpq-fe.h,postgres_ext.h;libpq.a,libpq.so和libpq.so.5到另外一台linux系统中。

6、写linux下的C++程序

例1:存储过程不返回游标

#include

#include

#include "libpq-fe.h"

static void exit_nicely(PGconn *conn) {     PQfinish(conn);     exit(1); }

int main(int argc, char **argv)

{  

const char *conninfo;  

PGconn     *conn;  

PGresult   *res;  

const char *result;  

const char *paramValues[1];    

conninfo = "host=192.168.0.182 dbname=XXDB user=XXuser password=XXpassword port=5432 connect_timeout=5";  

conn     = PQconnectdb(conninfo);  

if(PQstatus(conn) != CONNECTION_OK)  

{   

printf("Connection to server failed\n.");   

exit_nicely(conn);  

}  

paramValues[0] = "0";  

res = PQexecParams(conn,"select \"funSelectSnameBySno\"($1)",1,NULL,paramValues,NULL,NULL,0);

 if(PQresultStatus(res) != PGRES_TUPLES_OK)

 {   

printf("The Function Error.\n");   

PQclear(res);   

exit_nicely(conn);  

}  

else  

{   

result = PQgetvalue(res,0,0);   

printf("The Result is %s. \n",result);   

PQclear(res);   

exit_nicely(conn); 

 }  

return 0;

}

例2:存储过程返回游标

#include
#include
#include "libpq-fe.h"

static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

int main(int argc, char **argv)
{
 const char *conninfo;
 PGconn     *conn;
 PGresult   *res;
 const char *result;
 const char *paramValues[2];
 int     nFields;
 int        i,j;
 
 conninfo = "host=vnd-server dbname=VoiceNaviDB user=naviuser password=navipassword port=5432 connect_timeout=5";
 conn     = PQconnectdb(conninfo);
 if(PQstatus(conn) != CONNECTION_OK)
 {
  printf("Connection to server failed\n.");
  exit_nicely(conn);
 }
 paramValues[0] = "recordcur";
 paramValues[1] = "10";
 
 res = PQexec(conn, "BEGIN");
 if (PQresultStatus(res) != PGRES_COMMAND_OK)
 {
  printf("BEGIN command failed");
  PQclear(res);
  exit_nicely(conn);
 }
 
 res = PQexecParams(conn,"select \"funSelectBranchConditionBySid\"($1,$2)",2,NULL,paramValues,NULL,NULL,0);
 if(PQresultStatus(res) != PGRES_TUPLES_OK)
 {
  printf("The Function Error.\n");
  PQclear(res);
  exit_nicely(conn);
 }
 PQclear(res);
 res = PQexec(conn, "FETCH ALL in recordcur");
 if (PQresultStatus(res) != PGRES_TUPLES_OK)
 {
  printf("Fetch refcursor Error.");
  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 rows */
 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);

 return 0;
}

7、在linux上编译C程序:  

(1)gcc -I/linux/include/ -c voicetest1.c 生成voicetest1.o  其中-I/linux/include/需要根据libpq-fe.h,postgres_ext.h文件所在的具体路径设置。  

(2)gcc voicetest1.o -Llinux/lib/libpq.a -Llinux/lib -lpq -o b 生成可执行文件b;其中-Llinux/lib/也是需要根据具体路径来写。

8、生成b后运行./b,如果出现libpq.so.5共享库找不到的情况,可以用以下三种办法解决:  

(1)直接拷贝libpq.so.5库文件到/lib目录下,不过如果作为项目的话,这样做不太符合规则。  

(2)编辑 /etc/ld.so.conf把lib所在的具体路径加进去,然后运行一下ldconfig,这个需要root。  

(3)在你的用户下面,运行:export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH,然后再运行。(假如lib路径是/usr/local/pgsql/lib)

9、程序运行成功。

阅读(1973) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~