.
This commit is contained in:
		
							
								
								
									
										29
									
								
								Database.cpp
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								Database.cpp
									
									
									
									
									
								
							| @ -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); | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										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 | #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(); | ||||||
| }; | }; | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user