added basic connection with database
9791197 parent: b5cc0c0 modified
CMakeLists.txt +1 -1 | @@ -1,7 +1,7 @@ | ||
| 1 | 1 | cmake_minimum_required(VERSION 3.10) |
| 2 | 2 | project(fluffy) |
| 3 | 3 | |
| 4 | -set(CMAKE_CXX_STANDARD 11) | |
| 4 | +set(CMAKE_CXX_STANDARD 20) | |
| 5 | 5 | set(CMAKE_CXX_STANDARD_REQUIRED True) |
| 6 | 6 | |
| 7 | 7 | include_directories(${CMAKE_SOURCE_DIR}/include) |
| @@ -1,7 +1,7 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.10) | 1 | cmake_minimum_required(VERSION 3.10) |
| 2 | project(fluffy) | 2 | project(fluffy) |
| 3 | 3 | ||
| 4 | -set(CMAKE_CXX_STANDARD 11) | 4 | +set(CMAKE_CXX_STANDARD 20) |
| 5 | set(CMAKE_CXX_STANDARD_REQUIRED True) | 5 | set(CMAKE_CXX_STANDARD_REQUIRED True) |
| 6 | 6 | ||
| 7 | include_directories(${CMAKE_SOURCE_DIR}/include) | 7 | include_directories(${CMAKE_SOURCE_DIR}/include) |
modified
src/main.cpp +67 -6 | @@ -1,12 +1,51 @@ | ||
| 1 | 1 | #include <iostream> |
| 2 | 2 | #include <string> |
| 3 | 3 | #include <cstdlib> |
| 4 | +#include <filesystem> | |
| 4 | 5 | #include <../include/sqlite3.h> |
| 5 | 6 | |
| 6 | -const char* gamePath = ""; | |
| 7 | +std::string gamePath; | |
| 8 | + | |
| 9 | +std::string database_initialization_command = "CREATE TABLE GAME(" | |
| 10 | + "GAME_ID INT PRIMARY KEY NOT NULL, " | |
| 11 | + "GAME_NAME TEXT NOT NULL, " | |
| 12 | + "GAME_PATH TEXT NOT NULL);"; | |
| 13 | + | |
| 14 | +std::string insert_example_game_command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (1, 'Devastating Fog', \"\");"; | |
| 15 | + | |
| 16 | +const char* select_all_games_command = "SELECT * FROM GAME;"; | |
| 17 | + | |
| 18 | +bool debugRun = true; | |
| 19 | + | |
| 20 | +void printMessage(std::string message) { | |
| 21 | + if (debugRun) { | |
| 22 | + std::cout << message << std::endl; | |
| 23 | + } | |
| 24 | +} | |
| 25 | + | |
| 26 | +void execute_sql_command(sqlite3* database, std::string command) { | |
| 27 | + int exit = 0; | |
| 28 | + char* messageError; | |
| 29 | + exit = sqlite3_exec(database, database_initialization_command.c_str(), NULL, 0, &messageError); | |
| 30 | + | |
| 31 | + if (exit != SQLITE_OK) { | |
| 32 | + printMessage("Error with command: " + command); | |
| 33 | + sqlite3_free(messageError); | |
| 34 | + } | |
| 35 | +} | |
| 36 | + | |
| 37 | +void initialize_database(sqlite3* database) { | |
| 38 | + execute_sql_command(database, database_initialization_command.c_str()); | |
| 39 | + execute_sql_command(database, insert_example_game_command.c_str()); | |
| 40 | + printMessage("Database Initialized"); | |
| 41 | +} | |
| 7 | 42 | |
| 8 | 43 | int main() { |
| 44 | + bool newCreation = not std::filesystem::exists("example_database"); | |
| 45 | + | |
| 46 | + char* messageError; | |
| 9 | 47 | sqlite3* database; |
| 48 | + sqlite3_stmt* stmt; | |
| 10 | 49 | int exit = 0; |
| 11 | 50 | exit = sqlite3_open("example_database", &database); |
| 12 | 51 | |
| @@ -14,12 +53,34 @@ int main() { | ||
| 14 | 53 | std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; |
| 15 | 54 | return -1; |
| 16 | 55 | } |
| 17 | - else { | |
| 18 | - std::cout << "Opened database successfully!" << std::endl; | |
| 19 | - } | |
| 20 | - sqlite3_close(database); | |
| 21 | 56 | |
| 57 | + std::cout << "Opened database successfully!" << std::endl; | |
| 22 | 58 | |
| 23 | - //system(gamePath); | |
| 59 | + | |
| 60 | + | |
| 61 | + if (newCreation) { | |
| 62 | + initialize_database(database); | |
| 63 | + } | |
| 64 | + | |
| 65 | + exit = sqlite3_prepare_v2(database, select_all_games_command, -1, &stmt, 0); | |
| 66 | + | |
| 67 | + if (exit) { | |
| 68 | + std::cerr << "Error retrieving data" << sqlite3_errmsg(database) << std::endl; | |
| 69 | + sqlite3_close(database); | |
| 70 | + return -1; | |
| 71 | + } | |
| 72 | + | |
| 73 | + while (sqlite3_step(stmt) == SQLITE_ROW) { | |
| 74 | + int id = sqlite3_column_int(stmt, 0); | |
| 75 | + const unsigned char* game_name = sqlite3_column_text(stmt, 1); | |
| 76 | + const unsigned char* path = sqlite3_column_text(stmt, 2); | |
| 77 | + | |
| 78 | + std::cout << game_name << ": " << path << std::endl; | |
| 79 | + gamePath.append(reinterpret_cast<const char*>(path)); | |
| 80 | + } | |
| 81 | + sqlite3_finalize(stmt); | |
| 82 | + | |
| 83 | + sqlite3_close(database); | |
| 84 | + system(gamePath.c_str()); | |
| 24 | 85 | return 0; |
| 25 | 86 | } |
| \ No newline at end of file | ||
| @@ -1,12 +1,51 @@ | |||
| 1 | #include <iostream> | 1 | #include <iostream> |
| 2 | #include <string> | 2 | #include <string> |
| 3 | #include <cstdlib> | 3 | #include <cstdlib> |
| 4 | +#include <filesystem> | ||
| 4 | #include <../include/sqlite3.h> | 5 | #include <../include/sqlite3.h> |
| 5 | 6 | ||
| 6 | -const char* gamePath = ""; | 7 | +std::string gamePath; |
| 8 | + | ||
| 9 | +std::string database_initialization_command = "CREATE TABLE GAME(" | ||
| 10 | + "GAME_ID INT PRIMARY KEY NOT NULL, " | ||
| 11 | + "GAME_NAME TEXT NOT NULL, " | ||
| 12 | + "GAME_PATH TEXT NOT NULL);"; | ||
| 13 | + | ||
| 14 | +std::string insert_example_game_command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (1, 'Devastating Fog', \"\");"; | ||
| 15 | + | ||
| 16 | +const char* select_all_games_command = "SELECT * FROM GAME;"; | ||
| 17 | + | ||
| 18 | +bool debugRun = true; | ||
| 19 | + | ||
| 20 | +void printMessage(std::string message) { | ||
| 21 | + if (debugRun) { | ||
| 22 | + std::cout << message << std::endl; | ||
| 23 | + } | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +void execute_sql_command(sqlite3* database, std::string command) { | ||
| 27 | + int exit = 0; | ||
| 28 | + char* messageError; | ||
| 29 | + exit = sqlite3_exec(database, database_initialization_command.c_str(), NULL, 0, &messageError); | ||
| 30 | + | ||
| 31 | + if (exit != SQLITE_OK) { | ||
| 32 | + printMessage("Error with command: " + command); | ||
| 33 | + sqlite3_free(messageError); | ||
| 34 | + } | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +void initialize_database(sqlite3* database) { | ||
| 38 | + execute_sql_command(database, database_initialization_command.c_str()); | ||
| 39 | + execute_sql_command(database, insert_example_game_command.c_str()); | ||
| 40 | + printMessage("Database Initialized"); | ||
| 41 | +} | ||
| 7 | 42 | ||
| 8 | int main() { | 43 | int main() { |
| 44 | + bool newCreation = not std::filesystem::exists("example_database"); | ||
| 45 | + | ||
| 46 | + char* messageError; | ||
| 9 | sqlite3* database; | 47 | sqlite3* database; |
| 48 | + sqlite3_stmt* stmt; | ||
| 10 | int exit = 0; | 49 | int exit = 0; |
| 11 | exit = sqlite3_open("example_database", &database); | 50 | exit = sqlite3_open("example_database", &database); |
| 12 | 51 | ||
| @@ -14,12 +53,34 @@ int main() { | |||
| 14 | std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; | 53 | std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; |
| 15 | return -1; | 54 | return -1; |
| 16 | } | 55 | } |
| 17 | - else { | ||
| 18 | - std::cout << "Opened database successfully!" << std::endl; | ||
| 19 | - } | ||
| 20 | - sqlite3_close(database); | ||
| 21 | 56 | ||
| 57 | + std::cout << "Opened database successfully!" << std::endl; | ||
| 22 | 58 | ||
| 23 | - //system(gamePath); | 59 | + |
| 60 | + | ||
| 61 | + if (newCreation) { | ||
| 62 | + initialize_database(database); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + exit = sqlite3_prepare_v2(database, select_all_games_command, -1, &stmt, 0); | ||
| 66 | + | ||
| 67 | + if (exit) { | ||
| 68 | + std::cerr << "Error retrieving data" << sqlite3_errmsg(database) << std::endl; | ||
| 69 | + sqlite3_close(database); | ||
| 70 | + return -1; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + while (sqlite3_step(stmt) == SQLITE_ROW) { | ||
| 74 | + int id = sqlite3_column_int(stmt, 0); | ||
| 75 | + const unsigned char* game_name = sqlite3_column_text(stmt, 1); | ||
| 76 | + const unsigned char* path = sqlite3_column_text(stmt, 2); | ||
| 77 | + | ||
| 78 | + std::cout << game_name << ": " << path << std::endl; | ||
| 79 | + gamePath.append(reinterpret_cast<const char*>(path)); | ||
| 80 | + } | ||
| 81 | + sqlite3_finalize(stmt); | ||
| 82 | + | ||
| 83 | + sqlite3_close(database); | ||
| 84 | + system(gamePath.c_str()); | ||
| 24 | return 0; | 85 | return 0; |
| 25 | } | 86 | } |
| \ No newline at end of file | \ No newline at end of file | ||