Advertisement
Guest User

Untitled

a guest
May 16th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /*
  2. * DBtest.cpp
  3. * Accessing a Postgresql DB from within C++
  4. * 2016 / Örjan Sterner
  5. */
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <sstream> // ostringstream
  10. #include <iomanip>
  11. #include <clocale> // setlocale
  12. #include <cctype> // toupper
  13. #include "DBhandler.h"
  14. #include "winutf8.h"
  15. #include "libpq-fe.h"
  16. #include "terminal.h"
  17. #include "PWinput.h"
  18.  
  19.  
  20. using std::string;
  21. using std::ostringstream;
  22. using std::toupper;
  23. using std::cerr;
  24. using std::cout;
  25. using std::cin;
  26. using std::endl;
  27. using std::setw;
  28. using std::left;
  29.  
  30. bool logIn();
  31.  
  32. int main(int argc, char **argv)
  33. {
  34. Terminal term; // To manipulate colors
  35. std::setlocale(LC_ALL, "Swedish_Sweden.1252"); // To show swedish characters in windows console
  36. term.pushColor(TerminalColor(COLOR::CYAN, COLOR::BLACK));
  37. cout << "************* Postgresql C/C++ interface demo *************\n\n";
  38. term.popColor();
  39.  
  40. const string PG_PASSWORD { "kattfluff5" }; // Set your postgres password here
  41. ostringstream oss; // string stream to concatenate strings
  42. string utf8QueryStr; // string converted to UTF8
  43.  
  44. // Build string with connection info
  45. oss << "user=postgres password=" << PG_PASSWORD << " dbname=postgres hostaddr=127.0.0.1 port=5432";
  46.  
  47. win1252utf8(oss.str(), utf8QueryStr); // Convert to UTF8, result in utf8QueryStr
  48.  
  49. DBhandler db { utf8QueryStr }; // Create our database handler object
  50.  
  51. /* Make a connection to the database */
  52. bool opResult = db.connectDB();
  53. if (!opResult) {
  54. term.pushColor(TerminalColor(COLOR::RED, COLOR::BLACK));
  55. cerr << "Wrong creditials, you are not logged in to postgres. Exiting." << endl;
  56. term.clearColors();
  57. return 1;
  58. }
  59. term.pushColor(TerminalColor(COLOR::GREEN, COLOR::BLACK));
  60. cout << "Connected to Postgres DB on localhost\n\n";
  61.  
  62. /*---------------------------------------------------------------------------------------*/
  63.  
  64. // Let's show all persons
  65.  
  66.  
  67. // ORGINIAL START
  68. oss.str(""); // Reset the stream and make it empty
  69. //oss << "SELECT förnamn, efternamn, gatuadress, postnr, epost FROM sportsclub.person;";
  70. oss << "SELECT förnamn, efternamn, gatuadress, person.postnr, postort, epost FROM";
  71. oss << " sportsclub8.person, sportsclub8.postort WHERE person.postnr = postort.postnr;";
  72.  
  73. win1252utf8(oss.str(), utf8QueryStr); // convert to utf8
  74.  
  75.  
  76. // Start transaction
  77. if (!db.beginTransaction()) {
  78. cerr << "beginTransaction failed" << endl;
  79. db.closeDBconn();
  80. return 1;
  81. }
  82.  
  83. // Make the query
  84. PGresult* qResult = db.makeQuery(utf8QueryStr.c_str());
  85. if (!qResult) // Failure
  86. return 1;
  87.  
  88. // OK, let's get the field names, first get the number of fields
  89. int nFields = PQnfields(qResult);
  90.  
  91. // Prepare the header with employee table field name
  92. cout << "\nAll persons in Sportsclub8:" << endl;
  93. term.pushColor(TerminalColor(COLOR::YELLOW, COLOR::BLACK));
  94. cout << "\n******************************************************************************************************\n";
  95. for (int i = 0; i < nFields; i++) {
  96. string fieldStr;
  97. utf8win1252(PQfname(qResult, i), fieldStr); // convert from utf8
  98. fieldStr[0] = std::toupper(fieldStr[0]);
  99. cout << left << setw(10)<< fieldStr << "\t";
  100. }
  101. cout << endl;
  102.  
  103. // Next, print out the record for each row
  104. for (int i = 0; i < PQntuples(qResult); i++)
  105. {
  106. term.pushColor(TerminalColor(COLOR::WHITE, COLOR::BLACK));
  107. for (int j = 0; j < nFields; j++) {
  108. string valueStr; // utf8 convertered to Windows cp 1252
  109. utf8win1252(PQgetvalue(qResult, i, j), valueStr);
  110. cout << left << setw(10) << valueStr << "\t";
  111. }
  112. cout << endl;
  113. term.popColor();
  114. }
  115.  
  116. cout << "\n******************************************************************************************************\n";
  117. term.clearColors();
  118. PQclear(qResult);
  119.  
  120. db.endTransaction();
  121.  
  122. /* close the connection to the database and cleanup */
  123. db.closeDBconn();
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement