jeudi 27 août 2015

how to return pqxx connection and save into other variable?

// DataBaseConn.cpp
#include <iostream>
#include "yaml-cpp/yaml.h"
#include "DatabaseConn.h"

connection DatabaseConn::getConn() {

YAML::Node config = YAML::LoadFile("database.yaml");
std::string psql_user = config["production"]["user"].as<string>();
std::string psql_pass = config["production"]["password"].as<string>();
connection C("dbname=demo user="+ psql_user +" password="+ psql_pass +" hostaddr=127.0.0.1 port=5432");
if (C.is_open()) {
    std::cout << "Opened database successfully: " << C.dbname() << std::endl;
} else {
    std::cout << "Can't open database" << std::endl;
    throw "Database Connection Error";
}
return pqxx::basic_connection<connect_direct>(C);

}

and trying to save connection in other variable for further use

 connection conn = DatabaseConn().getConn();

can it possible and what i'm doing wrong ? I'm novice in C++.

Compile Error:

/usr/local/include/pqxx/basic_connection.hxx:54:40: error: within this context
template<typename CONNECTPOLICY> class basic_connection :
                                    ^
main.cpp: In function ‘int main()’:
main.cpp:20:50: note: synthesized method ‘pqxx::basic_connection<pqxx::connect_direct>::basic_connection(const pqxx::basic_connection<pqxx::connect_direct>&)’ first required here 
     connection conn = DatabaseConn().getConn();

Aucun commentaire:

Enregistrer un commentaire