This commit is contained in:
Debucquoy Anthony 2021-12-29 16:00:32 +01:00
parent 5269c53dc8
commit f63416b787
3 changed files with 27 additions and 17 deletions

View File

@ -3,24 +3,31 @@
Database::Database(){
Config conf("config.txt");
std::ostringstream pgsqlConnectingString;
std::string pgsqlConnectingString;
pgsqlConnectingString
<< " host=" << conf.getValue("db_host")
<< " dbname=" << conf.getValue("db_name")
<< " user=" << conf.getValue("db_user")
<< " password=" << conf.getValue("db_pass");
pgsqlConnectingString
.append(" host=").append(conf.getValue("db_host"))
.append(" dbname=").append(conf.getValue("db_name"))
.append(" user=").append(conf.getValue("db_user"))
.append(" password=").append(conf.getValue("db_pass"));
std::cout << pgsqlConnectingString << std::endl;
dbconn = new pqxx::connection(pgsqlConnectingString.str());
dbconn = PQconnectdb(pgsqlConnectingString.c_str());
}
pqxx::result Database::execute(std::string cmd){
pqxx::work w(*dbconn);
pqxx::result r(w.exec(cmd));
w.commit();
void Database::execute(std::string cmd){
PQexec(dbconn, cmd.c_str());
}
PGresult* Database::fetch(std::string cmd){
PGresult *r = PQexec(dbconn, cmd.c_str());
return r;
// Don't forget to close after using this request
}
void Database::disconnect(){
dbconn->close();
PQfinish(dbconn);
}

View File

@ -1,4 +1,7 @@
#include <pqxx/pqxx>
#include <string>
#include <iostream>
#include <libpq-fe.h>
#include <vector>
#ifndef DATABASE_H
#define DATABASE_H
@ -7,12 +10,14 @@ class Database
{
private:
pqxx::connection *dbconn;
PGconn *dbconn;
public:
Database();
pqxx::result execute(std::string cmd);
void execute(std::string cmd);
PGresult* fetch(std::string cmd);
void disconnect();
};

View File

@ -12,8 +12,6 @@ Database db;
Network net;
net.run();
}