Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. //Koncsik Milán - JXWFDH
  2. #include <iostream>
  3. #include <pqxx/pqxx>
  4. #include "tinyxml2.h"
  5. #include <list>
  6. #include <sstream>
  7. #include <string>
  8.  
  9. #ifndef XMLCheckResult
  10.     #define XMLCheckResult(a_eResult) if (a_eResult != XML_SUCCESS) { printf("Error: %i\n", a_eResult); return a_eResult; }
  11. #endif
  12.  
  13.  
  14. using namespace std;
  15. using namespace pqxx;
  16. using namespace tinyxml2;
  17.  
  18. void createTable(connection C)
  19. {
  20.   char * sql = "create table places(name varchar(32) primary key,ptype varchar(32) not null,cname varchar(32) not null,scname varchar(32) not null,area int not null,habitant int  not null, rooms int not null);";
  21.   work W(C);
  22.   W.exec(sql);
  23.   W.commit();
  24.   cout<<"Tábla létrehozása sikeres."<<endl;
  25. }
  26.  
  27. int main(int argc, char* argv[])
  28. {
  29.  
  30.     string file_in="output.xml";
  31.     string a,b,c,d;
  32.     int e,f,g;
  33.     stringstream ss;
  34.     float progress = 0.0;
  35.     int barWidth = 70;
  36.  
  37.    try
  38.     {
  39.       connection C("dbname = dbmkoncsik user = mkoncsik password = 123dbpass hostaddr = 195.70.37.11 port = 6081");
  40.       if (C.is_open()) {
  41.          cout << "Adatbázis: "<<C.dbname()<<" elérése sikeres. "<<endl;
  42.       } else {
  43.          cout << "Adatbázis elérése sikertelen." << endl;
  44.          return 1;
  45.       }
  46.  
  47.       XMLDocument *xmlDoc = new XMLDocument;
  48.       XMLError eResult = xmlDoc->LoadFile(file_in.c_str());
  49.       XMLCheckResult(eResult);
  50.       XMLNode *pRoot = xmlDoc->RootElement();
  51.       if (pRoot == nullptr) return XML_ERROR_FILE_READ_ERROR;
  52.       XMLElement *pElement = pRoot->FirstChildElement("place");
  53.       if (pElement == nullptr) return XML_ERROR_PARSING_ELEMENT;
  54.       XMLElement * pChildElement;
  55.  
  56.           while(pElement)
  57.           {
  58.               a=pElement->ToElement()->Attribute("name");
  59.               pChildElement = pElement->FirstChildElement("ptype");
  60.               b=pChildElement->GetText();
  61.               pChildElement = pElement->FirstChildElement("cname");
  62.               c=pChildElement->GetText();
  63.               pChildElement = pElement->FirstChildElement("scname");
  64.               d=pChildElement->GetText();
  65.               pChildElement = pElement->FirstChildElement("area");
  66.               e=atoi(pChildElement->GetText());
  67.               pChildElement = pElement->FirstChildElement("habitant");
  68.               f=atoi(pChildElement->GetText());
  69.               pChildElement = pElement->FirstChildElement("rooms");
  70.               g=atoi(pChildElement->GetText());
  71.               if(b.length()>32)b=b.substr(0,14);
  72.               ss<<"insert into places (name,ptype,cname,scname,area,habitant,rooms) values('"<<a<<"','"<<b<<"','"<<c<<"','"<<d<<"',"<<e<<","<<f<<","<<g<<")";
  73.               work W(C);
  74.               W.exec( ss.str().c_str() );
  75.               W.commit();
  76.               ss.str("");
  77.               pElement=pElement->NextSiblingElement();
  78.               progress += 0.00031486146;
  79.               cout << "] " << int(progress * 100.0) << " %\r";
  80.               cout << "[";
  81.               int pos = barWidth * progress;
  82.               for (int i = 0; i < barWidth; ++i)
  83.               {
  84.                   if (i < pos) cout << "=";
  85.                   else if (i == pos) cout << ">";
  86.                   else cout << " ";
  87.               }
  88.               cout.flush();
  89.           }
  90.           cout<<"Sikeres feltöltés."<<endl;
  91.  
  92.       C.disconnect ();
  93.    } catch (const std::exception &e)
  94.     {
  95.       cerr << e.what() << std::endl;
  96.       return 1;
  97.    }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement