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(){ Database::Database(){
Config conf("config.txt"); Config conf("config.txt");
std::ostringstream pgsqlConnectingString; std::string pgsqlConnectingString;
pgsqlConnectingString pgsqlConnectingString
<< " host=" << conf.getValue("db_host") .append(" host=").append(conf.getValue("db_host"))
<< " dbname=" << conf.getValue("db_name") .append(" dbname=").append(conf.getValue("db_name"))
<< " user=" << conf.getValue("db_user") .append(" user=").append(conf.getValue("db_user"))
<< " password=" << conf.getValue("db_pass"); .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){ void Database::execute(std::string cmd){
pqxx::work w(*dbconn); PQexec(dbconn, cmd.c_str());
pqxx::result r(w.exec(cmd)); }
w.commit();
PGresult* Database::fetch(std::string cmd){
PGresult *r = PQexec(dbconn, cmd.c_str());
return r; return r;
// Don't forget to close after using this request
} }
void Database::disconnect(){ 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 #ifndef DATABASE_H
#define DATABASE_H #define DATABASE_H
@ -7,12 +10,14 @@ class Database
{ {
private: private:
pqxx::connection *dbconn; PGconn *dbconn;
public: public:
Database(); Database();
pqxx::result execute(std::string cmd); void execute(std::string cmd);
PGresult* fetch(std::string cmd);
void disconnect(); void disconnect();
}; };

View File

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