Parkmanne/db.cpp

53 lines
2.0 KiB
C++
Raw Normal View History

2019-06-25 00:09:33 +00:00
#include "headers/db.h"
2019-06-25 00:42:08 +00:00
namespace data {
char* error_message; // no clue, prolly error message
static int callback(void* NotUsed, int argc, char** argv, char** azColName) {
int i;
for (i = 0; i < argc; i++) {
std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << "\n";
}
std::cout << "\n";
return 0;
}
void open_db(sqlite3* db) {
2019-06-25 01:09:43 +00:00
int status = sqlite3_open("database.sqlite", &db); // for some reason, 0 is pass and 1 is erroring out
// so it prolly shouldn't be called status
if (status) {
2019-06-25 00:42:08 +00:00
std::cout << "Can't open database: " << sqlite3_errmsg(db) << "\n";
}
2019-06-25 00:09:33 +00:00
}
2019-06-25 00:42:08 +00:00
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 primary key, name text, card_code varchar(20), verhicle int)";
const char* parkspot_table = "create table Park_spot (id int primary key, taken boolean, customer_id int)";
const char* parktime_table = "create table Park_time (id int primary key, customer_id int, spot_id int, start real, end real, duration real)";
2019-06-25 00:42:08 +00:00
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
}
2019-06-25 00:09:33 +00:00
2019-06-25 00:42:08 +00:00
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
}
}
2019-06-25 00:09:33 +00:00
2019-06-25 00:42:08 +00:00
} // namespace data