Database class added

This commit is contained in:
Martin Brodbeck 2018-07-06 13:30:23 +02:00
parent d8b39eab0a
commit 1e8c4eac5f
3 changed files with 49 additions and 1 deletions

View File

@ -1,13 +1,17 @@
find_package(Boost 1.62 REQUIRED)
find_package(SQLite3 REQUIRED)
set(CORE_HEADERS
entity.h
database.h
)
set(CORE_SOURCES
entity.cpp
database.cpp
)
add_library(core STATIC ${CORE_SOURCES})
target_link_libraries(core Boost::boost)
target_link_libraries(core Boost::boost)
target_link_libraries(core sqlite3)

24
src/core/database.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "database.h"
#include <stdexcept>
Database::Database(const std::string& dbname) : db(nullptr)
{
const int errCode = sqlite3_open(dbname.c_str(), &db);
if(errCode) {
throw std::runtime_error("Could not open database file.");
}
}
Database::~Database()
{
sqlite3_close(db);
}
void Database::exec(const std::string& sql)
{
const int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, nullptr);
if(errCode) {
throw std::runtime_error("Error in SQL execution.");
}
}

20
src/core/database.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef DATABASE_H
#define DATABASE_H
#include <string>
#include <sqlite3.h>
class Database
{
public:
Database(const std::string& dbname);
~Database();
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
void exec(const std::string& sql);
private:
sqlite3 *db;
};
#endif // DATABASE_H