Parkmanne/headers/db.h
2019-06-24 21:09:33 -03:00

56 lines
1.7 KiB
C++

#include <sqlite3.h>
#include <string>
#include <iostream>
namespace db
{
static int callback(void *NotUsed, int argc, char **argv, char **azColName);
sqlite3* db;
char* error_message;
void open_db(sqlite3* db_){
int success = sqlite3_open("sqlite.db", &db);
if (!success) {
std::cout << "Can't open database: "<<sqlite3_errmsg(db)<<"\n";
}
}
// stupid lib
void initialize_db(sqlite3* db_){
// ugh, sqlite3_exec doesn't accept const strings, so i have to use const char*
const char* customer_table = "create table Customer (id int, name varchar(50), card_code varchar(20), verhicle int)";
const char* parkspot_table = "create table Park_spot (id int, taken boolean, customer_id int)";
const char* parktime_table = "create table Park_time (id int, customer_id int, spot_id int, start real, end real, duration real)";
int success;
success = sqlite3_exec(db_, customer_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db_, parkspot_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db_, parktime_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
}
}