C++ code to connect Postgres Database

/* 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;
        }
    }
};

No comments:

Post a Comment

If you have any doubts, please let me know.

Various types of salts effect on health (discussion with gemenie)

Is  rock salt or bitnoon helps in digestion or just a airuvedic nuska This is for informational purposes only. For medical advice or diagnos...