juk3n/fluffypublic Fork
⑂ 9791197
Commits
⬇ Clone ▾
git clone https://git.rickub.com/juk3n/fluffy.git
git clone ssh://git@rickub.com/juk3n/fluffy.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

added basic connection with database

juk3n committed 2024-06-06T16:58:15+02:00 Browse files
9791197 parent: b5cc0c0
modified CMakeLists.txt +1 -1
@@ -1,7 +1,7 @@
11 cmake_minimum_required(VERSION 3.10)
22 project(fluffy)
33
4-set(CMAKE_CXX_STANDARD 11)
4+set(CMAKE_CXX_STANDARD 20)
55 set(CMAKE_CXX_STANDARD_REQUIRED True)
66
77 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 @@
11 #include <iostream>
22 #include <string>
33 #include <cstdlib>
4+#include <filesystem>
45 #include <../include/sqlite3.h>
56
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+}
742
843 int main() {
44+ bool newCreation = not std::filesystem::exists("example_database");
45+
46+ char* messageError;
947 sqlite3* database;
48+ sqlite3_stmt* stmt;
1049 int exit = 0;
1150 exit = sqlite3_open("example_database", &database);
1251
@@ -14,12 +53,34 @@ int main() {
1453 std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl;
1554 return -1;
1655 }
17- else {
18- std::cout << "Opened database successfully!" << std::endl;
19- }
20- sqlite3_close(database);
2156
57+ std::cout << "Opened database successfully!" << std::endl;
2258
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());
2485 return 0;
2586 }
\ 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