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.

Regarding site Free Browser Tool (https://free-browser-tools.mohitkrgupta.dev) (discussion with google search AI)

Various discussions with Google Search AI regarding site  Free Browser Tool are as below: site free-browser-tools.mohitkrgupta.dev what doe...