I am using libpqxx to connect to a postgres database by creating a class.
class databaseConnection
{
public:
pqxx::connection* conn;
void SetConnection(){
conn=new pqxx::connection(
"username=temp "
"host=db.corral.tacc.utexas.edu "
"password=timelione "
"dbname=temp");
}
void Disconnect(){
conn->disconnect();
}
pqxx::result query(std::string strSQL){
//SetConnection();
pqxx::work trans(*conn,"trans");
pqxx::result res=trans.exec(strSQL);
trans.commit();
return res;
}
};
int main()
{
databaseConnection* pdatabase;
pdatabase->SetConnection();
return 0;
}
I am getting error that says
terminate called after throwing an instance of 'pqxx::broken_connection'
what(): invalid connection option "database"
Can anyone help me out?
Thanks
pgxx::connection(const PGSTD::string&)is basically a wrapper around libpq'sPQconnectdb()function, so the supported connection parameter keywords are the same as for libpq.The parameter key word for the PostgreSQL user name to connect as is
user, notusername. Perhaps correcting that will fix the problem.Also, in your sample code,
pdatabaseis an uninitialized pointer. You could either allocate adatabaseConnectionobject on the stack with:or use
newto heap-allocate adatabaseConnectionobject:But you need to pick one.