[英]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++.
我是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();
The connection object is probably not copyable nor movable and thus cannot be returned by value. 连接对象可能是不可复制或不可移动的,因此无法通过值返回。 Consider allocating it on the heap via operator new and return a pointer to it:
考虑通过new运算符在堆上分配它,并返回指向它的指针:
connection *DatabaseConn::getConn()
{
// ...omitted...
return new pqxx::basic_connection<connect_direct>(C);
}
The caller is responsible for deleting the returned connection. 调用方负责删除返回的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.