about summary refs log blame commit diff stats
path: root/src/sqliteinterface.cpp
blob: 328a44c7f62f833e78d60d884096bccc3504b704 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                    


                     
                    
                              
 


                               
                                
 

                                   
 

                                            
                              
 




                                                    
 

                                                 




                                                
                                    
     



                                 
                      









                                     
 
#include <sqlite3.h>
#include <iostream>
#include <cstring>
#include <filesystem>
#include <stdexcept>
#include "sqliteinterface.hpp"

namespace fs = std::filesystem;
using namespace std;

void SqliteInterface::openDB() {

    fs::path appdata = DEF_APPDATA;
    if ( ! fs::exists(appdata) ) {

        error_code ec;
        fs::create_directories(appdata, ec);
        if (ec.value() != 0) {

            char errmsg[] = "Can't create folder: ";
            strcat(errmsg, ec.message().c_str());
            throw runtime_error(errmsg);
        }
    }

    fs::path filename = appdata / "ytcpp.db";
    int rc = sqlite3_open(filename.c_str(), &db);

    if( rc ) {

        char errmsg[] = "Can't open database: ";
        strcat(errmsg, sqlite3_errmsg(db));
        throw runtime_error(errmsg);
    }
}

void SqliteInterface::closeDB() {

    sqlite3_close(db);
}

SqliteInterface::SqliteInterface() {

    openDB();
}

SqliteInterface::~SqliteInterface() {

    closeDB();
}