DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > PostgreSQL > 驱动及连接

PostgreSQL连接C/C++接口实例

本教程讲解C/C++连接PostgreSQL,使用libpqxx库,这是官方的C++客户端API用于连接PostgreSQL。libpqxx源代码在BSD许可下,可以免费下载,传递给他人,改变它或出售,它包括在你自己的代码,并分享你的代码更改。

安装

libpqxx最新版本的可供下载链接下载libpqxx。所以下载的最新版本,并遵循以下步骤:Download Libpqxx.

  1. wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz 
  2. tar xvfz libpqxx-4.0.tar.gz 
  3. cd libpqxx-4.0 
  4. ./configure 
  5. make 
  6. make install 

在开始使用C/C++ PostgreSQL界面,找到PostgreSQL安装目录pg_hba.conf文件中添加下面一行:

  1. # IPv4 local connections: 
  2. host    all         all         127.0.0.1/32          md5 

可以start/restart Postgres的服务器的情况下,它没有运行使用下面的命令:

  1. [root@host]# service postgresql restart 
  2. Stopping postgresql service:                               [  OK  ] 
  3. Starting postgresql service:      

C/C++ APIs

以下是重要接口例程可满足工作需求与PostgreSQL数据库的C/C + +程序。如果正在寻找一个更复杂的应用程序,那么可以寻找到libpqxx官方文档,或者可以使用商用的API。




连接到数据库

以下C代码段说明了如何在本地机器上运行端口5432连接到一个现有的数据库。在这里用斜线\续行。

  1. #include <iostream> 
  2. #include <pqxx/pqxx>  
  3.  
  4. using namespace std; 
  5. using namespace pqxx; 
  6.  
  7. int main(int argc, char* argv[]) 
  8.    try{ 
  9.       connection C("dbname=testdb user=postgres password=cohondob \ 
  10.       hostaddr=127.0.0.1 port=5432"); 
  11.       if (C.is_open()) { 
  12.          cout << "Opened database successfully: " << C.dbname() << endl; 
  13.       } else { 
  14.          cout << "Can't open database" << endl; 
  15.          return 1; 
  16.       } 
  17.       C.disconnect (); 
  18.    }catch (const std::exception &e){ 
  19.       cerr << e.what() << std::endl; 
  20.       return 1; 
  21.    } 


现在,让我们编译和运行上面的程序,使用用户postgres和密码pass123访问连接到我们的数据库testdb。可以使用基于数据库设置用户ID和密码。在给定的顺序,记住要保持使用-lpqxx和-plq!否则,链接器将提示缺少以"pq"开始的函数名字。

  1. $g++ test.cpp -lpqxx -lpq 
  2. $./a.out 
  3. Opened database successfully: testdb 



创建表

下面的C代码段将被用于先前创建的数据库中创建一个表:

  1. #include <iostream> 
  2. #include <pqxx/pqxx>  
  3.  
  4. using namespace std; 
  5. using namespace pqxx; 
  6.  
  7. int main(int argc, char* argv[]) 
  8.    char * sql; 
  9.     
  10.    try{ 
  11.       connection C("dbname=testdb user=postgres password=cohondob \ 
  12.       hostaddr=127.0.0.1 port=5432"); 
  13.       if (C.is_open()) { 
  14.          cout << "Opened database successfully: " << C.dbname() << endl; 
  15.       } else { 
  16.          cout << "Can't open database" << endl; 
  17.          return 1; 
  18.       } 
  19.       /* Create SQL statement */ 
  20.       sql = "CREATE TABLE COMPANY("  \ 
  21.       "ID INT PRIMARY KEY     NOT NULL," \ 
  22.       "NAME           TEXT    NOT NULL," \ 
  23.       "AGE            INT     NOT NULL," \ 
  24.       "ADDRESS        CHAR(50)," \ 
  25.       "SALARY         REAL );"; 
  26.  
  27.       /* Create a transactional object. */ 
  28.       work W(C); 
  29.        
  30.       /* Execute SQL query */ 
  31.       W.exec( sql ); 
  32.       W.commit(); 
  33.       cout << "Table created successfully" << endl; 
  34.       C.disconnect (); 
  35.    }catch (const std::exception &e){ 
  36.       cerr << e.what() << std::endl; 
  37.       return 1; 
  38.    } 
  39.  
  40.    return 0; 


上述程序编译和执行时,它会在testdb数据库,并创建COMPANY 表会显示下面的语句:

  1. Opened database successfully: testdb 
  2. Table created successfully 


插入操作

下面的C代码段显示了我们如何能够在上面的例子中创建COMPANY 表中的记录:

  1. #include <iostream> 
  2. #include <pqxx/pqxx>  
  3.  
  4. using namespace std; 
  5. using namespace pqxx; 
  6.  
  7. int main(int argc, char* argv[]) 
  8.    char * sql; 
  9.     
  10.    try{ 
  11.       connection C("dbname=testdb user=postgres password=cohondob \ 
  12.       hostaddr=127.0.0.1 port=5432"); 
  13.       if (C.is_open()) { 
  14.          cout << "Opened database successfully: " << C.dbname() << endl; 
  15.       } else { 
  16.          cout << "Can't open database" << endl; 
  17.          return 1; 
  18.       } 
  19.       /* Create SQL statement */ 
  20.       sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \ 
  21.       "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \ 
  22.       "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \ 
  23.       "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     \ 
  24.       "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \ 
  25.       "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \ 
  26.       "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \ 
  27.       "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );"; 
  28.  
  29.       /* Create a transactional object. */ 
  30.       work W(C); 
  31.        
  32.       /* Execute SQL query */ 
  33.       W.exec( sql ); 
  34.       W.commit(); 
  35.       cout << "Records created successfully" << endl; 
  36.       C.disconnect (); 
  37.    }catch (const std::exception &e){ 
  38.       cerr << e.what() << std::endl; 
  39.       return 1; 
  40.    } 
  41.  
  42.    return 0; 


上述程序编译和执行时,它会创建COMPANY表中的记录,并会显示以下两行:

  1. Opened database successfully: testdb 
  2. Records created successfully 

原文链接:http://www.yiibai.com/html/postgresql/2013/080894.html


本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号