1 storing global paths to games in database #2
Merged@github-juk3n wants to merge
1-storing-global-paths-to-games-in-database into mainMerged as
c10208d66020.4 files changed
modified
.gitignore +2 -1 | @@ -1,2 +1,3 @@ | ||
| 1 | 1 | *.vscode* |
| 2 | -hello | |
| \ No newline at end of file | ||
| 2 | +build/* | |
| 3 | +*sqlite* | |
| \ No newline at end of file | ||
| @@ -1,2 +1,3 @@ | |||
| 1 | *.vscode* | 1 | *.vscode* |
| 2 | -hello | ||
| \ No newline at end of file | \ No newline at end of file | ||
| 2 | +build/* | ||
| 3 | +*sqlite* | ||
| \ No newline at end of file | \ No newline at end of file | ||
added
CMakeLists.txt +9 -0 | new file mode 100644 | ||
| @@ -0,0 +1,9 @@ | ||
| 1 | +cmake_minimum_required(VERSION 3.10) | |
| 2 | +project(fluffy) | |
| 3 | + | |
| 4 | +set(CMAKE_CXX_STANDARD 20) | |
| 5 | +set(CMAKE_CXX_STANDARD_REQUIRED True) | |
| 6 | + | |
| 7 | +include_directories(${CMAKE_SOURCE_DIR}/include) | |
| 8 | + | |
| 9 | +add_executable(fluffy src/main.cpp src/sqlite3.c) | |
| \ No newline at end of file | ||
| new file mode 100644 | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | +cmake_minimum_required(VERSION 3.10) | ||
| 2 | +project(fluffy) | ||
| 3 | + | ||
| 4 | +set(CMAKE_CXX_STANDARD 20) | ||
| 5 | +set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
| 6 | + | ||
| 7 | +include_directories(${CMAKE_SOURCE_DIR}/include) | ||
| 8 | + | ||
| 9 | +add_executable(fluffy src/main.cpp src/sqlite3.c) | ||
| \ No newline at end of file | \ No newline at end of file | ||
deleted
main.cpp +0 -10 | deleted file mode 100644 | ||
| @@ -1,10 +0,0 @@ | ||
| 1 | -#include <iostream> | |
| 2 | -#include <string> | |
| 3 | -#include <cstdlib> | |
| 4 | - | |
| 5 | -const char* gamePath = ""; | |
| 6 | - | |
| 7 | -int main() { | |
| 8 | - std::cout << "Hello" << std::endl; | |
| 9 | - system(gamePath); | |
| 10 | -} | |
| \ No newline at end of file | ||
| deleted file mode 100644 | |||
| @@ -1,10 +0,0 @@ | |||
| 1 | -#include <iostream> | ||
| 2 | -#include <string> | ||
| 3 | -#include <cstdlib> | ||
| 4 | - | ||
| 5 | -const char* gamePath = ""; | ||
| 6 | - | ||
| 7 | -int main() { | ||
| 8 | - std::cout << "Hello" << std::endl; | ||
| 9 | - system(gamePath); | ||
| 10 | -} | ||
| \ No newline at end of file | \ No newline at end of file | ||
added
src/main.cpp +86 -0 | new file mode 100644 | ||
| @@ -0,0 +1,86 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +#include <string> | |
| 3 | +#include <cstdlib> | |
| 4 | +#include <filesystem> | |
| 5 | +#include <../include/sqlite3.h> | |
| 6 | + | |
| 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 | +} | |
| 42 | + | |
| 43 | +int main() { | |
| 44 | + bool newCreation = not std::filesystem::exists("example_database"); | |
| 45 | + | |
| 46 | + char* messageError; | |
| 47 | + sqlite3* database; | |
| 48 | + sqlite3_stmt* stmt; | |
| 49 | + int exit = 0; | |
| 50 | + exit = sqlite3_open("example_database", &database); | |
| 51 | + | |
| 52 | + if (exit) { | |
| 53 | + std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; | |
| 54 | + return -1; | |
| 55 | + } | |
| 56 | + | |
| 57 | + std::cout << "Opened database successfully!" << std::endl; | |
| 58 | + | |
| 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()); | |
| 85 | + return 0; | |
| 86 | +} | |
| \ No newline at end of file | ||
| new file mode 100644 | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | +#include <iostream> | ||
| 2 | +#include <string> | ||
| 3 | +#include <cstdlib> | ||
| 4 | +#include <filesystem> | ||
| 5 | +#include <../include/sqlite3.h> | ||
| 6 | + | ||
| 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 | +} | ||
| 42 | + | ||
| 43 | +int main() { | ||
| 44 | + bool newCreation = not std::filesystem::exists("example_database"); | ||
| 45 | + | ||
| 46 | + char* messageError; | ||
| 47 | + sqlite3* database; | ||
| 48 | + sqlite3_stmt* stmt; | ||
| 49 | + int exit = 0; | ||
| 50 | + exit = sqlite3_open("example_database", &database); | ||
| 51 | + | ||
| 52 | + if (exit) { | ||
| 53 | + std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; | ||
| 54 | + return -1; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + std::cout << "Opened database successfully!" << std::endl; | ||
| 58 | + | ||
| 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()); | ||
| 85 | + return 0; | ||
| 86 | +} | ||
| \ No newline at end of file | \ No newline at end of file | ||