自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PostgreSQL連接C/C++接口實(shí)例

開發(fā) 后端 前端 PostgreSQL
本教程講解C/C++連接PostgreSQL,使用libpqxx庫,這是官方的C++客戶端API用于連接PostgreSQL。libpqxx源代碼在BSD許可下,可以免費(fèi)下載,傳遞給他人,改變它或出售,它包括在你自己的代碼,并分享你的代碼更改。

本教程講解C/C++連接PostgreSQL,使用libpqxx庫,這是官方的C++客戶端API用于連接PostgreSQL。libpqxx源代碼在BSD許可下,可以免費(fèi)下載,傳遞給他人,改變它或出售,它包括在你自己的代碼,并分享你的代碼更改。

安裝

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的服務(wù)器的情況下,它沒有運(yùn)行使用下面的命令:

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

C/C++ APIs

以下是重要接口例程可滿足工作需求與PostgreSQL數(shù)據(jù)庫的C/C + +程序。如果正在尋找一個(gè)更復(fù)雜的應(yīng)用程序,那么可以尋找到libpqxx官方文檔,或者可以使用商用的API。

 

 

連接到數(shù)據(jù)庫

以下C代碼段說明了如何在本地機(jī)器上運(yùn)行端口5432連接到一個(gè)現(xiàn)有的數(shù)據(jù)庫。在這里用斜線\續(xù)行。

  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.    } 

 

現(xiàn)在,讓我們編譯和運(yùn)行上面的程序,使用用戶postgres和密碼pass123訪問連接到我們的數(shù)據(jù)庫testdb。可以使用基于數(shù)據(jù)庫設(shè)置用戶ID和密碼。在給定的順序,記住要保持使用-lpqxx和-plq!否則,鏈接器將提示缺少以"pq"開始的函數(shù)名字。

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

#p#

創(chuàng)建表

下面的C代碼段將被用于先前創(chuàng)建的數(shù)據(jù)庫中創(chuàng)建一個(gè)表:

  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; 

 

上述程序編譯和執(zhí)行時(shí),它會在testdb數(shù)據(jù)庫,并創(chuàng)建COMPANY 表會顯示下面的語句:

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

插入操作

下面的C代碼段顯示了我們?nèi)绾文軌蛟谏厦娴睦又袆?chuàng)建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; 

 

上述程序編譯和執(zhí)行時(shí),它會創(chuàng)建COMPANY表中的記錄,并會顯示以下兩行:

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

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

責(zé)任編輯:陳四芳 來源: yiibai.com
相關(guān)推薦

2010-01-26 09:50:30

C++接口

2009-08-31 17:35:12

C#接口實(shí)例

2021-10-11 11:53:07

C++接口代碼

2021-06-10 09:40:12

C++性能優(yōu)化Linux

2019-08-28 14:21:39

C++C接口代碼

2020-07-31 18:33:56

C++編程語言

2009-08-31 18:17:32

C#接口編程

2009-08-27 17:59:56

C#接口定義

2010-01-14 17:13:53

C++接口

2014-01-02 15:30:56

PostgreSQLJava

2010-05-14 15:23:03

2010-01-11 09:56:07

C++編程實(shí)例

2009-08-31 17:16:12

C#實(shí)現(xiàn)接口

2009-08-27 17:40:21

C#接口的作用

2009-08-31 17:30:10

C#接口的作用

2011-07-14 17:45:06

CC++

2010-07-08 10:28:51

UML接口

2010-11-22 16:01:08

C++多態(tài)

2011-09-16 10:00:56

C++

2010-01-21 14:07:14

CC++聲明
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號