Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Koncsik Milán - JXWFDH
- #include <iostream>
- #include <pqxx/pqxx>
- #include "tinyxml2.h"
- #include <list>
- #include <sstream>
- #include <string>
- #ifndef XMLCheckResult
- #define XMLCheckResult(a_eResult) if (a_eResult != XML_SUCCESS) { printf("Error: %i\n", a_eResult); return a_eResult; }
- #endif
- using namespace std;
- using namespace pqxx;
- using namespace tinyxml2;
- void createTable(connection C)
- {
- 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);";
- work W(C);
- W.exec(sql);
- W.commit();
- cout<<"Tábla létrehozása sikeres."<<endl;
- }
- int main(int argc, char* argv[])
- {
- string file_in="output.xml";
- string a,b,c,d;
- int e,f,g;
- stringstream ss;
- float progress = 0.0;
- int barWidth = 70;
- try
- {
- connection C("dbname = dbmkoncsik user = mkoncsik password = 123dbpass hostaddr = 195.70.37.11 port = 6081");
- if (C.is_open()) {
- cout << "Adatbázis: "<<C.dbname()<<" elérése sikeres. "<<endl;
- } else {
- cout << "Adatbázis elérése sikertelen." << endl;
- return 1;
- }
- XMLDocument *xmlDoc = new XMLDocument;
- XMLError eResult = xmlDoc->LoadFile(file_in.c_str());
- XMLCheckResult(eResult);
- XMLNode *pRoot = xmlDoc->RootElement();
- if (pRoot == nullptr) return XML_ERROR_FILE_READ_ERROR;
- XMLElement *pElement = pRoot->FirstChildElement("place");
- if (pElement == nullptr) return XML_ERROR_PARSING_ELEMENT;
- XMLElement * pChildElement;
- while(pElement)
- {
- a=pElement->ToElement()->Attribute("name");
- pChildElement = pElement->FirstChildElement("ptype");
- b=pChildElement->GetText();
- pChildElement = pElement->FirstChildElement("cname");
- c=pChildElement->GetText();
- pChildElement = pElement->FirstChildElement("scname");
- d=pChildElement->GetText();
- pChildElement = pElement->FirstChildElement("area");
- e=atoi(pChildElement->GetText());
- pChildElement = pElement->FirstChildElement("habitant");
- f=atoi(pChildElement->GetText());
- pChildElement = pElement->FirstChildElement("rooms");
- g=atoi(pChildElement->GetText());
- if(b.length()>32)b=b.substr(0,14);
- ss<<"insert into places (name,ptype,cname,scname,area,habitant,rooms) values('"<<a<<"','"<<b<<"','"<<c<<"','"<<d<<"',"<<e<<","<<f<<","<<g<<")";
- work W(C);
- W.exec( ss.str().c_str() );
- W.commit();
- ss.str("");
- pElement=pElement->NextSiblingElement();
- progress += 0.00031486146;
- cout << "] " << int(progress * 100.0) << " %\r";
- cout << "[";
- int pos = barWidth * progress;
- for (int i = 0; i < barWidth; ++i)
- {
- if (i < pos) cout << "=";
- else if (i == pos) cout << ">";
- else cout << " ";
- }
- cout.flush();
- }
- cout<<"Sikeres feltöltés."<<endl;
- C.disconnect ();
- } catch (const std::exception &e)
- {
- cerr << e.what() << std::endl;
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement