/* At first install libpqxx in linux*/
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
class GetDbConn
{
public:
static connection *getPostgresDbConnection(const std::string& conStr)
{
pqxx::connection *c=new connection(conStr);
return c;
}
};
----------------
/*Code to call this connection is as follows
#include <pqxx/pqxx>
#include "getDbConn.h"
using namespace std;
using namespace pqxx;
class MyDb
{
public:
static connection * getTradingNseConnection()
{
char const *conStr="dbname = <database_name> user = <user_name> password = <db_password> hostaddr = 127.0.0.1 port = 5432";
pqxx::connection *con=GetDbConn::getPostgresDbConnection(conStr);
if ((*con).is_open())
{
cout << "Opened database successfully: " << (*con).dbname() << endl;
return con;
}
else
{
cout << "Can't open database" << endl;
return NULL;
}
}
};
Comments
Post a Comment
If you have any doubts, please let me know.