-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (55 loc) · 1.65 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: Hp
*
* Created on September 17, 2018, 2:40 PM
*/
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
pqxx::result query(char *argv[]) {
std::string host = argv[1];
std::string db = argv[2];
std::string user = argv[3];
std::string pass = argv[4];
std::string query = argv[5];
pqxx::connection c{"dbname = "+db+" user = "+user+" password = "+pass+" host = "+host+" port = 5432"};
pqxx::work txn{c};
pqxx::result r = txn.exec(query);
bool printHeader = true;
for (auto row = r.begin(); row != r.end(); row++) {
if (printHeader) {
for (auto field = row.begin(); field != row.end(); field++) {
if (printHeader) {
std::cout << field->name() << '|';
}
}
std::cout << std::endl;
}
for (auto field = row.begin(); field != row.end(); field++) {
std::cout << field->c_str() << '|';
}
printHeader = false;
std::cout << std::endl;
}
txn.commit();
return r;
}
int main(int, char *argv[]) {
try {
pqxx::result r = query(argv);
} catch (const pqxx::sql_error &e) {
std::cerr << "SQL error: " << e.what() << std::endl;
std::cerr << "Query was: " << e.query() << std::endl;
return 2;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}