small changes

This commit is contained in:
Martin Brodbeck 2018-07-11 08:33:40 +02:00
parent 8d58da4618
commit 0f3e7ba799

View file

@ -9,16 +9,20 @@ Database::Database(const std::string& dbname) : db_(nullptr)
if (errCode) {
throw std::runtime_error("Could not open database file.");
}
exec("PRAGMA foreign_key = 1");
sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY);
}
Database::~Database() { sqlite3_close(db_); }
void Database::exec(const std::string& sql)
{
const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, nullptr);
char* errMsg;
const int errCode = sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &errMsg);
if (errCode) {
throw std::runtime_error("Error in SQL execution.");
std::string errMsgString(errMsg); // Make a C++ string of the errMsg, so that we can call
// sqlite3_free() before throwing the exception
sqlite3_free(errMsg);
throw std::runtime_error("Error in SQL execution: " + errMsgString);
}
}