mi024

College project "Projet IAD" master 1
git clone https://esimon.eu/repos/mi024.git
Log | Files | Refs | README

sqlite_utils.hpp (750B)


      1 #ifndef SQLITE_UTILS_HPP_INCLUDED
      2 #define SQLITE_UTILS_HPP_INCLUDED
      3 
      4 #include <string>
      5 #include <stdexcept>
      6 
      7 #include <sqlite3.h>
      8 
      9 /** @brief SBRM struct for a sqlite connection. */
     10 struct DB_connection{
     11 	DB_connection(std::string const &database_path): handle(0) {
     12 		if(sqlite3_open(database_path.c_str(), &handle) != SQLITE_OK)
     13 			throw std::runtime_error(sqlite3_errmsg(handle));
     14 	}
     15 
     16 	~DB_connection(){
     17 		if(handle && sqlite3_close(handle) != SQLITE_OK)
     18 			throw std::runtime_error(sqlite3_errmsg(handle));
     19 	}
     20 
     21 	sqlite3 *handle;
     22 };
     23 
     24 /** @brief SBRM struct for a sqlite statement. */
     25 struct DB_statement{
     26 	DB_statement(sqlite3_stmt *stmt): stmt(stmt) {}
     27 
     28 	~DB_statement(){
     29 		if(stmt)
     30 			sqlite3_finalize(stmt);
     31 	}
     32 
     33 	sqlite3_stmt *stmt;
     34 };
     35 
     36 #endif
     37