Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include "../../include/warehouseData/dataBase.h"
  2.  
  3. void whData::initDB() {
  4. conn = PQconnectdb("host='dbm.fe.up.pt' user='siem1719' password='ii.2018' dbname='siem1719' port='5432'");
  5.  
  6. if (!conn) {
  7. cout << "Failed to connect to the database" << endl;
  8. exit(-1);
  9. }
  10.  
  11. if (PQstatus(conn) != CONNECTION_OK) {
  12. cout << "Failed to connect to the database" << endl;
  13. exit(-1);
  14. }
  15. }
  16.  
  17. PGresult* whData::executeSQL(string sql)
  18. {
  19. PGresult* res = PQexec(conn, sql.c_str());
  20. if (!(PQresultStatus(res) == PGRES_COMMAND_OK || PQresultStatus(res) == PGRES_TUPLES_OK)){
  21. cout << "Failed to execute the command: " << sql << endl;
  22. cout << PQresultErrorMessage(res) << '\n';
  23. return NULL;
  24. }
  25.  
  26. return res;
  27. }
  28.  
  29. void whData::closeDB() {
  30. PQfinish(conn);
  31. }
  32.  
  33. string int_to_string(int i){
  34. stringstream ss;
  35. ss << i;
  36. std::string str = ss.str();
  37. return str;
  38. }
  39.  
  40. int whData::getStock(int part) {
  41. string buffer = int_to_string(part);
  42. //itoa(part,buff,10);
  43. PGresult* res = executeSQL("SELECT COUNT(*) FROM II.part WHERE type = ('"+ buffer+"')");
  44. int qty = atoi(PQgetvalue(res,0,1));
  45.  
  46. return qty;
  47. }
  48.  
  49. void whData::updateStock() {
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement