Possible to load basic data from the database
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "crow.h"
|
||||
#include "database.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Database::Database() :
|
||||
m_db(nullptr)
|
||||
{}
|
||||
@@ -9,6 +11,15 @@ Database::~Database() {
|
||||
sqlite3_close(m_db);
|
||||
}
|
||||
|
||||
sqlite3_stmt* Database::prepareStmt(const string& sql){
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
CROW_LOG_ERROR << "Failed to prepare statement: " << sqlite3_errmsg(m_db);
|
||||
return nullptr;
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
bool Database::exec(const char* sqlQuery) {
|
||||
char* errmsg = nullptr;
|
||||
int rc = sqlite3_exec(m_db, sqlQuery, nullptr, nullptr, &errmsg);
|
||||
@@ -20,13 +31,72 @@ bool Database::exec(const char* sqlQuery) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Database::exec(const std::string& sqlQuery)
|
||||
{
|
||||
exec(sqlQuery.c_str());
|
||||
bool Database::exec(const std::string& sqlQuery){
|
||||
return exec(sqlQuery.c_str());
|
||||
}
|
||||
|
||||
map<string, string> Database::getStrMap(const string& sql){
|
||||
sqlite3_stmt* stmt = prepareStmt(sql);
|
||||
map<string, string> map;
|
||||
if (stmt == nullptr)
|
||||
return map;
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
string key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
string value = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
map[key] = value;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return map;
|
||||
}
|
||||
|
||||
set<string> Database::getStrSet(const string& sql){
|
||||
sqlite3_stmt* stmt = prepareStmt(sql);
|
||||
set<string> vec;
|
||||
if (stmt == nullptr)
|
||||
return vec;
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
string s = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
vec.insert(s);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return vec;
|
||||
}
|
||||
|
||||
std::optional<int64_t> Database::getInt(const char* sql) {
|
||||
sqlite3_stmt* stmt = prepareStmt(sql);
|
||||
if (stmt == nullptr)
|
||||
return {};
|
||||
|
||||
std::optional<int64_t> id;
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
id = sqlite3_column_int64(stmt, 0);
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return id;
|
||||
}
|
||||
|
||||
std::optional<int64_t> Database::insert(const char* sql) {
|
||||
sqlite3_stmt* stmt = prepareStmt(sql);
|
||||
if (stmt == nullptr)
|
||||
return {};
|
||||
|
||||
if (sqlite3_step(stmt) != SQLITE_DONE) {
|
||||
CROW_LOG_ERROR << "Insert failed: " << sqlite3_errmsg(m_db);
|
||||
sqlite3_finalize(stmt);
|
||||
return {};
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return sqlite3_last_insert_rowid(m_db);
|
||||
}
|
||||
|
||||
bool Database::open(){
|
||||
int rc = sqlite3_open("example.db", &m_db);
|
||||
int rc = sqlite3_open("app.db", &m_db);
|
||||
if (rc) {
|
||||
CROW_LOG_ERROR << "Can't open database: " << sqlite3_errmsg(m_db);
|
||||
return false;
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
#include "sqlite3.h"
|
||||
#include <string>
|
||||
|
||||
#include <optional>
|
||||
#include <set>
|
||||
class Database {
|
||||
|
||||
public:
|
||||
@@ -14,7 +15,18 @@ public:
|
||||
bool exec(const char* sqlQuery);
|
||||
bool exec(const std::string& sqlQuery);
|
||||
|
||||
/// returns true if the sql statment returns at least one row
|
||||
std::optional<int64_t> getInt(const char* sql);
|
||||
|
||||
std::optional<int64_t> insert(const char* sql);
|
||||
|
||||
std::set<std::string> getStrSet(const std::string& sql);
|
||||
|
||||
std::map<std::string, std::string> Database::getStrMap(const std::string& sql);
|
||||
|
||||
private:
|
||||
sqlite3_stmt* prepareStmt(const std::string& sql);
|
||||
|
||||
sqlite3* m_db;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user