䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL誂毖C/C++毖埼垂冋

PostgreSQL誂毖C/C++毖埼垂冋

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

橸嘺䇔昍䘋螾蓼C/C++誂毖PostgreSQL嘪䫘libpqxx康認滇垻桹䔇C++垵潙䆇API䫘庯誂毖PostgreSQL㔗libpqxx溊傼乕婘BSD螩埇婋埇傖噉蘹婋蘘嚹锐䂍傡庺櫹埻垄潡庺嫞垄寙拸婘嘹躻噌䔇傼乕幽彖庆嘹䔇傼乕敘櫹㔗 www.gitbook.net

垬輙

libpqxx橔桄䬽橸䔇埇冕婋蘘鷆毖婋蘘libpqxx㔗欔傖婋蘘䔇橔桄䬽橸幽镕冻傖婋準黴Download Libpqxx

gitbook.net

wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz
tar xvfz libpqxx-4.0.tar.gz
cd libpqxx-4.0
./configure
make
make install gitbook.net 

婘嚔哋嘪䫘C/C++ PostgreSQL䘯麵欆彄PostgreSQL垬輙䕞嘘pg_hba.conf桺傽婺時媹婋麵婔臯 gitbook.net

# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
 

gitbook.net

埇傖start/restart Postgres䔇橉媇単䔇愙喕婋垄澇橬誊臯嘪䫘婋麵䔇变傴

gitbook.net

[root@host]# service postgresql restart
Stopping postgresql service:                               [  OK  ]
Starting postgresql service:                               [  OK  ] www.gitbook.net 

C/C++ APIs

傖婋滇麉襕毖埼冋䘋埇悇轿噖嘩驔挗婯PostgreSQL昄扞康䔇C/C + +䘋废㔗套悩溼婘凂欆婔婻敘崉溗䔇庫䫘䘋废闼幽埇傖凂欆彄libpqxx垻桹桺懼潡蔙埇傖嘪䫘嘖䫘䔇API㔗 www.gitbook.net

S.N. API & 柟誄
1 pqxx::connection C( const std::string & dbstring ) gitbook.net

認滇儖䫘庯誂毖彄昄扞康婔婻䌂傋垔幬㔗認麯dbstring柊冕欔驔襕䔇埗昄冋套誂毖彄昄扞康 dbname=testdb user=postgres password=pass123 hostaddr=127.0.0.1 port=5432. www.gitbook.net

套悩誂毖螆䘞潊媘闼幽垄录傺C婯誂毖凹茇柊冕劇䓉橬䫘䔇庘昄噸噌庘昄㔗

www.gitbook.net

2 C.is_open()

is_open()滇婔婻誂毖凹茇䔇噸噌桹濘幽誫啂婄儫唚㔗套悩誂毖崇庯昂媘䪽攕彍溴桹濘誫啂true劥彍誫啂false㔗

gitbook.net

3 C.disconnect()

嘪䫘溴桹濘欷嚔䔇昄扞康誂毖桺嚔㔗 www.gitbook.net

4 pqxx::work W( C )

認滇婔婻䌂傋垔幬儖䫘庯录傺婔婻庋媇凹茇嘪䫘C誂毖桹嚟橔䂽儖赆䫘庯欓臯SQL臺埖䔇庋媇昇嚟㔗

gitbook.net

套悩庴滷凹茇赆录傺潊媘闼幽垄赆彖陉彄埻麟W認儖赆䫘準螪閞䕩噿䔇庋媇攓凹茇䔇噸噌桹濘㔗 www.gitbook.net

5 W.exec(const std::string & sql) 

認䓉傯庋媇凹茇䔇噸噌桹濘儖赆䫘庯欓臯SQL臺埖㔗 gitbook.net

6 W.commit()

認䓉傯庋媇凹茇䔇噸噌桹濘儖䫘庯柊庴庋媇㔗 gitbook.net

7 W.abort()

認䓉傯庋媇凹茇䔇噸噌桹濘儖䫘庯啂悔庋媇㔗 www.gitbook.net

8 pqxx::nontransaction N( C )

認滇婔婻䌂傋垔幬赆䫘準录傺嘪䫘C誂毖桹嚟橔䂽儖赆䫘庯婘麂庋媇攓昇嚟婋欓臯SQL臺埖䔇麂庋媇攓凹茇㔗

gitbook.net

套悩庴滷凹茇赆录傺潊媘闼幽垄赆彖陉彄埻麟N儖䫘庯螪閞䕩噿䔇麂庋媇攓凹茇䔇噸噌桹濘㔗 www.gitbook.net

9 N.exec(const std::string & sql) 

傯麂庋媇攓凹茇䔇噸噌桹濘儖赆䫘庯欓臯SQL臺埖幽誫啂婔婻䂷悩凹茇認垂鍙婪滇婔婻䔇誺傼単誫啂䔇螄嘘㔗

gitbook.net

誂毖彄昄扞康

傖婋C傼乕枕臘滯庖套嘘婘橸婄橺単婪誊臯䆇埼5432誂毖彄婔婻䯄橬䔇昄扞康㔗婘認麯䫘桩亪\䂺臯㔗 gitbook.net

#include <iostream>
#include <pqxx/pqxx> 

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
   try{
      connection C("dbname=testdb user=postgres password=cohondob \
      hostaddr=127.0.0.1 port=5432");
      if (C.is_open()) {
         cout << "Opened database successfully: " << C.dbname() << endl;
      } else {
         cout << "Can't open database" << endl;
         return 1;
      }
      C.disconnect ();
   }catch (const std::exception &e){
      cerr << e.what() << std::endl;
      return 1;
   }
} www.gitbook.net 

䯄婘螷潏傸䚡臏启誊臯婪麵䔇䘋废嘪䫘䫘潙postgres启凖乕pass123螪閞誂毖彄潏傸䔇昄扞康testdb㔗埇傖嘪䫘嘺庯昄扞康螆䘞䫘潙ID启凖乕㔗婘䂍垔䔇釺废螄嘟襕媺毕嘪䫘-lpqxx启-plq劥彍鷆毖単儖柊䴺䚺儏傖"pq"嚔哋䔇庘昄劉庖㔗

www.gitbook.net

$g++ test.cpp -lpqxx -lpq
$./a.out
Opened database successfully: testdb
 gitbook.net 

录傺臘

婋麵䔇C傼乕枕儖赆䫘庯噽嬉录傺䔇昄扞康婺录傺婔婻臘

www.gitbook.net

#include <iostream>
#include <pqxx/pqxx> 

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
   char * sql;
   
   try{
      connection C("dbname=testdb user=postgres password=cohondob \
      hostaddr=127.0.0.1 port=5432");
      if (C.is_open()) {
         cout << "Opened database successfully: " << C.dbname() << endl;
      } else {
         cout << "Can't open database" << endl;
         return 1;
      }
      /* Create SQL statement */
      sql = "CREATE TABLE COMPANY("  \
      "ID INT PRIMARY KEY     NOT NULL," \
      "NAME           TEXT    NOT NULL," \
      "AGE            INT     NOT NULL," \
      "ADDRESS        CHAR(50)," \
      "SALARY         REAL );";

      /* Create a transactional object. */
      work W(C);
      
      /* Execute SQL query */
      W.exec( sql );
      W.commit();
      cout << "Table created successfully" << endl;
      C.disconnect ();
   }catch (const std::exception &e){
      cerr << e.what() << std::endl;
      return 1;
   }

   return 0;
} 

www.gitbook.net

婪誄䘋废䚡臏启欓臯施垄嚔婘testdb昄扞康幽录傺COMPANY 臘嚔滆䴺婋麵䔇臺埖  www.gitbook.net

Opened database successfully: testdb
Table created successfully
 gitbook.net 

某噖淉嘩

婋麵䔇C傼乕枕滆䴺庖潏傸套嘘脘崘婘婪麵䔇冋床婺录傺COMPANY 臘婺䔇螄嘘

www.gitbook.net

#include <iostream>
#include <pqxx/pqxx> 

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
   char * sql;
   
   try{
      connection C("dbname=testdb user=postgres password=cohondob \
      hostaddr=127.0.0.1 port=5432");
      if (C.is_open()) {
         cout << "Opened database successfully: " << C.dbname() << endl;
      } else {
         cout << "Can't open database" << endl;
         return 1;
      }
      /* Create SQL statement */
      sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
      "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
      "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
      "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     \
      "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
      "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
      "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
      "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";

      /* Create a transactional object. */
      work W(C);
      
      /* Execute SQL query */
      W.exec( sql );
      W.commit();
      cout << "Records created successfully" << endl;
      C.disconnect ();
   }catch (const std::exception &e){
      cerr << e.what() << std::endl;
      return 1;
   }

   return 0;
} www.gitbook.net 

婪誄䘋废䚡臏启欓臯施垄嚔录傺COMPANY臘婺䔇螄嘘幽嚔滆䴺傖婋婴臯

www.gitbook.net

Opened database successfully: testdb
Records created successfully

 www.gitbook.net 

橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL誂毖C/C++毖埼垂冋
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080894.html
婪婔䇺PostgreSQL庖严婾庘昄      婋婔䇺PostgreSQL誂毖JAVA毖埼