Prepare statement

format
mbandic 2 weeks ago
parent 5fde5c52aa
commit 20f415026f
  1. 48
      .vscode/settings.json
  2. 3
      lib/sqlite3.hpp
  3. 43
      src/sqlite3.cpp
  4. 2
      test/test.cpp

@ -10,6 +10,52 @@
"type_traits": "cpp",
"tuple": "cpp",
"array": "cpp",
"utility": "cpp"
"utility": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"atomic": "cpp",
"bit": "cpp",
"chrono": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"map": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}

@ -34,7 +34,8 @@ public:
SQLite3(const string path, bool _keepOpen = false);
map<string, vector<string>> query(const string sql_command);
// map<string, vector<string>> query(const string sql_command);
std::map<std::string, std::vector<std::string>> query(const std::string& sql_command, const std::vector<std::string>& values);
~SQLite3();

@ -23,7 +23,7 @@ bool SQLite3::close() {
db = NULL;
return operationStatus != SQLITE_OK;
}
/*
map<string, vector<string>> SQLite3::query(const string sql_command) {
io.lock();
if (!keepOpen) {
@ -68,7 +68,48 @@ map<string, vector<string>> SQLite3::query(const string sql_command) {
io.unlock();
return _parsed;
}
*/
map<string, vector<string>> SQLite3::query(const string& sql_command, const vector<string>& values) {
lock_guard<mutex> lock(io);
map<string, vector<string>> results;
if (!keepOpen) {
if (open()) {
throw string("[ERROR] Unable to open database ");
}
}
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql_command.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
throw string("[ERROR] Failed to prepare statement: ") + sqlite3_errmsg(db);
}
// Binduj parametre
for (size_t i = 0; i < values.size(); ++i) {
sqlite3_bind_text(stmt, static_cast<int>(i + 1), values[i].c_str(), -1, SQLITE_TRANSIENT);
}
// Dohvati rezultate
int columnCount = sqlite3_column_count(stmt);
while (sqlite3_step(stmt) == SQLITE_ROW) {
for (int i = 0; i < columnCount; ++i) {
const char* columnName = sqlite3_column_name(stmt, i);
const char* columnValue = (const char*)sqlite3_column_text(stmt, i);
results[columnName].push_back(columnValue ? columnValue : "NULL");
}
}
sqlite3_finalize(stmt); // Oslobodi resurse vezane uz izjavu
if (!keepOpen) {
if (close()) {
throw string("[ERROR] Unable to close database ");
}
}
return results;
}
map<string, vector<string>> SQLite3::parse(const string& result) {
string a = result;

@ -13,7 +13,7 @@ int main() {
// cout << mydb.ask("Select * from Tab1");
// cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');");
auto res = mydb.query("Select * from Tab1");
auto res = mydb.query("Select * from Tab1 Where id > ?", {"3"});
// cout << endl << res["NAME"][1];
// cout << endl << res["MAIL"][1];

Loading…
Cancel
Save