Compare commits

..

No commits in common. "4fd86cc606069f17c390e4ee709edc851738906d" and "745df5d48bfbe198a1c9c96d725990f2dde695e0" have entirely different histories.

4 changed files with 37 additions and 27 deletions

BIN
example/example copy.db Normal file

Binary file not shown.

View File

@ -10,7 +10,6 @@
#include <map> #include <map>
// #include <algorithm> // #include <algorithm>
#include <iostream> #include <iostream>
#include <mutex>
using namespace std; using namespace std;
@ -25,13 +24,15 @@ class sql3 {
bool keepOpen; bool keepOpen;
string Answer; string Answer;
map<string, vector<string>> parsed; map<string, vector<string>> parsed;
mutex io;
sql3(const string path, bool _keepOpen = false); sql3(const string path, bool _keepOpen = false);
bool open(); bool open();
bool close(); bool close();
bool run(const string sql_command);
string answer();
void mapit(); void mapit();
string ask(const string sql_command);
map<string, vector<string>> query(const string sql_command); map<string, vector<string>> query(const string sql_command);
~sql3(); ~sql3();

View File

@ -23,11 +23,10 @@ bool sql3::close() {
return operationStatus != SQLITE_OK; return operationStatus != SQLITE_OK;
} }
map<string, vector<string>> sql3::query(const string sql_command) { bool sql3::run(const string sql_command) {
io.lock();
if (!keepOpen) { if (!keepOpen) {
if (open()) { if (open()) {
io.unlock();
throw string("[ERROR] Unable to open database "); throw string("[ERROR] Unable to open database ");
} }
} }
@ -38,6 +37,7 @@ map<string, vector<string>> sql3::query(const string sql_command) {
exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError);
if (exec_stuts != SQLITE_OK) { if (exec_stuts != SQLITE_OK) {
// printf("[ERROR] Ne mogu čitati bazu podataka!");
sqlite3_free(messaggeError); sqlite3_free(messaggeError);
r = false; r = false;
} }
@ -48,25 +48,40 @@ map<string, vector<string>> sql3::query(const string sql_command) {
if (!keepOpen) { if (!keepOpen) {
if(close()) { if(close()) {
io.unlock();
throw string("[ERROR] Unable to close database "); throw string("[ERROR] Unable to close database ");
} }
} }
map<string, vector<string>> _parsed; return r;
}
if (!r) { string sql3::answer() {
io.unlock(); string _Answer = Answer;
Answer.clear();
return _Answer;
}
string sql3::ask(const string sql_command) {
if (!run(sql_command)) {
throw string("[ERROR] Unable to read database "); throw string("[ERROR] Unable to read database ");
//return {};
} }
else {
return answer();
}
}
map<string, vector<string>> sql3::query(const string sql_command) {
if (!run(sql_command)) {
throw string("[ERROR] Unable to read database ");
//return {};
}
else { else {
mapit(); mapit();
_parsed = parsed; map<string, vector<string>> _parsed = parsed;
parsed.clear(); parsed.clear();
return _parsed;
} }
io.unlock();
return _parsed;
} }
void sql3::mapit() { void sql3::mapit() {

View File

@ -5,27 +5,21 @@
using namespace std; using namespace std;
int main() { int main() {
try {
sql3 mydb("../example/example_copy.db", false); sql3 mydb("../example/example.db", false);
// cout << mydb.ask("Select * from Tab1"); // cout << mydb.ask("Select * from Tab1");
// cout << mydb.ask("INSERT INTO Tab1 VALUES(3,'Pakora', 'marijanab@bitelex.ml');"); // 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");
auto res = mydb.query("INSERT INTO Tab1 VALUES(4,'Mare', 'marijanab@bitelex.co');");
// for (auto i : res)
// for (auto j: i.second)
// cout << i.first << " : " << j << endl;
// cout << endl << res["NAME"][1]; // cout << endl << res["NAME"][1];
// cout << endl << res["MAIL"][1];
for (auto i : res) {
for (auto j: i.second) {
cout << i.first << " : " << j << endl;
}
}
} catch (const string err) {
cout << err << endl;
}
return 0; return 0;
} }