橸嘺䇔昍䘋螾蓼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 md5gitbook.net
埇傖start/restart Postgres䔇橉媇単䔇愙喕婋垄澇橬誊臯嘪䫘婋麵䔇变傴
[root@host]# service postgresql restart Stopping postgresql service: [ OK ] Starting postgresql service: [ OK ] www.gitbook.net
傖婋滇麉襕毖埼冋䘋埇悇轿噖嘩驔挗婯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"嚔哋䔇庘昄劉庖㔗
$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