Linux下libpq開發

《原創文章,如需轉載請註明作者及出處》在linux下C/C++連接遠程postgresql數據庫詳細步驟

1、下載postgres數據庫8.3.1版本,下載網站:http://www.postgresql.org/

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 <stdio.h>

#include <stdlib.h>

#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 <stdio.h>
#include <stdlib.h>
#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、程序運行成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章