-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteDB.cpp
103 lines (92 loc) · 3.05 KB
/
SQLiteDB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "SQLiteDB.h"
SQLiteDB::SQLiteDB(const char* filename) : db(nullptr) {
int rc = sqlite3_open(filename, &db);
if (rc != SQLITE_OK) {
std::cout << "Error opening SQLite database: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
db = nullptr;
}
}
SQLiteDB::~SQLiteDB() {
if (db != nullptr) {
sqlite3_close(db);
db = nullptr;
}
}
bool SQLiteDB::executeQuery(const char *query) {
char* errorMessage = nullptr;
int rc = sqlite3_exec(db, query, nullptr, nullptr, &errorMessage);
if (rc != SQLITE_OK) {
std::cerr << "Error executing SQL query: " << errorMessage << std::endl;
sqlite3_free(errorMessage);
return false;
}
return true;
}
bool SQLiteDB::insertData(const char *tableName, const std::vector<std::string> &values) {
std::string query = "INSERT INTO ";
query += tableName;
query += " (\"name\", \"password\", \"balance\")";
query += " VALUES (";
for (size_t i = 0; i < values.size(); ++i) {
if (i > 0) query += ", ";
query += "'";
query += values[i];
query += "'";
}
query += ");";
return executeQuery(query.c_str());
}
std::vector<std::vector<std::string>> SQLiteDB::selectData(const char *tableName, const int id) {
std::vector<std::vector<std::string>> result;
std::string query = "SELECT * FROM ";
query += tableName;
query += " WHERE \"ID\" = '";
query += std::to_string(id);
query += "' ;";
sqlite3_stmt* statement;
int rc = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "Error preparing SQL statement: " << sqlite3_errmsg(db) << std::endl;
return result;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
std::vector<std::string> row;
int columns = sqlite3_column_count(statement);
for (int i = 0; i < columns; ++i) {
row.push_back(reinterpret_cast<const char*>(sqlite3_column_text(statement, i)));
}
result.push_back(row);
}
sqlite3_finalize(statement);
return result;
}
bool SQLiteDB::updateData(const char *tableName, const char *columnName, const char *columnValue, const int id) {
std::string query = "UPDATE ";
query += tableName;
query += " SET ";
query += columnName;
query += " = '";
query += columnValue;
query += "' WHERE \"ID\" = ";
query += std::to_string(id);
query += ";";
return executeQuery(query.c_str());
}
int SQLiteDB::getMaxID(const char *tableName) {
std::string query = "SELECT MAX(ID) FROM ";
query += tableName;
query += ";";
sqlite3_stmt* statement;
int maxID = 0;
int rc = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);
if (rc != SQLITE_OK) {
std::cerr << "Error preparing SQL statement: " << sqlite3_errmsg(db) << std::endl;
return maxID;
}
if (sqlite3_step(statement) == SQLITE_ROW) {
maxID = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
return maxID;
}