婋麵䔇C傼乕枕臘滯潏傸套嘘脘崘诙埡幽滆䴺婘婪麵䔇冋床婺录傺COMPANY臘䔇螄嘘
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 = "SELECT * from COMPANY"; /* Create a non-transactional object. */ nontransaction N(C); /* Execute SQL query */ result R( N.exec( sql )); /* List down all the records */ for (result::const_iterator c = R.begin(); c != R.end(); ++c) { cout << "ID = " << c[0].as<int>() << endl; cout << "Name = " << c[1].as<string>() << endl; cout << "Age = " << c[2].as<int>() << endl; cout << "Address = " << c[3].as<string>() << endl; cout << "Salary = " << c[4].as<float>() << endl; } cout << "Operation done successfully" << endl; C.disconnect (); }catch (const std::exception &e){ cerr << e.what() << std::endl; return 1; } return 0; }gitbook.net
婪誄䘋废䚡臏启欓臯施垄嚔库䫘傖婋䂷悩 gitbook.net
Opened database successfully: testdb ID = 1 Name = Paul Age = 32 Address = California Salary = 20000 ID = 2 Name = Allen Age = 25 Address = Texas Salary = 15000 ID = 3 Name = Teddy Age = 23 Address = Norway Salary = 20000 ID = 4 Name = Mark Age = 25 Address = Rich-Mond Salary = 65000 Operation done successfullygitbook.net
婋麵䔇C傼乕枕滆䴺庖潏傸套嘘嘪䫘UPDATE臺埖準敘桄傂嘘螄嘘䇽劯傯COMPANY臘诙埡幽滆䴺敘桄䔇螄嘘
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 a transactional object. */ work W(C); /* Create SQL UPDATE statement */ sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1"; /* Execute SQL query */ W.exec( sql ); W.commit(); cout << "Records updated successfully" << endl; /* Create SQL SELECT statement */ sql = "SELECT * from COMPANY"; /* Create a non-transactional object. */ nontransaction N(C); /* Execute SQL query */ result R( N.exec( sql )); /* List down all the records */ for (result::const_iterator c = R.begin(); c != R.end(); ++c) { cout << "ID = " << c[0].as<int>() << endl; cout << "Name = " << c[1].as<string>() << endl; cout << "Age = " << c[2].as<int>() << endl; cout << "Address = " << c[3].as<string>() << endl; cout << "Salary = " << c[4].as<float>() << endl; } cout << "Operation done successfully" << endl; C.disconnect (); }catch (const std::exception &e){ cerr << e.what() << std::endl; return 1; } return 0; }www.gitbook.net
婪誄䘋废䚡臏启欓臯施垄嚔库䫘傖婋䂷悩
gitbook.net
Opened database successfully: testdb
Records updated successfully
ID = 2
Name = Allen
Age = 25
Address = Texas
Salary = 15000
ID = 3
Name = Teddy
Age = 23
Address = Norway
Salary = 20000
ID = 4
Name = Mark
Age = 25
Address = Rich-Mond
Salary = 65000
ID = 1
Name = Paul
Age = 32
Address = California
Salary = 25000
Operation done successfully
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 a transactional object. */ work W(C); /* Create SQL DELETE statement */ sql = "DELETE from COMPANY where ID = 2"; /* Execute SQL query */ W.exec( sql ); W.commit(); cout << "Records deleted successfully" << endl; /* Create SQL SELECT statement */ sql = "SELECT * from COMPANY"; /* Create a non-transactional object. */ nontransaction N(C); /* Execute SQL query */ result R( N.exec( sql )); /* List down all the records */ for (result::const_iterator c = R.begin(); c != R.end(); ++c) { cout << "ID = " << c[0].as<int>() << endl; cout << "Name = " << c[1].as<string>() << endl; cout << "Age = " << c[2].as<int>() << endl; cout << "Address = " << c[3].as<string>() << endl; cout << "Salary = " << c[4].as<float>() << endl; } cout << "Operation done successfully" << endl; C.disconnect (); }catch (const std::exception &e){ cerr << e.what() << std::endl; return 1; } return 0; }gitbook.net
婪誄䘋废䚡臏启欓臯施垄嚔库䫘傖婋䂷悩
Opened database successfully: testdb Records deleted successfully ID = 3 Name = Teddy Age = 23 Address = Norway Salary = 20000 ID = 4 Name = Mark Age = 25 Address = Rich-Mond Salary = 65000 ID = 1 Name = Paul Age = 32 Address = California Salary = 25000 Operation done successfullygitbook.net