.
This commit is contained in:
parent
5269c53dc8
commit
f63416b787
31
Database.cpp
31
Database.cpp
@ -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);
|
||||
}
|
||||
|
11
Database.hpp
11
Database.hpp
@ -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();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user