Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
marcelb | 5fde5c52aa | 1 week ago |
marcelb | 9fa9178e32 | 1 year ago |
marcelb | 4fd86cc606 | 1 year ago |
marcelb | d3726df3e3 | 1 year ago |
marcelb | 8e9abb6e56 | 1 year ago |
marcelb | 745df5d48b | 1 year ago |
marcelb | 11348539d3 | 1 year ago |
marcelb | 3a67bf47c2 | 1 year ago |
marcelb | f6c8443c5d | 1 year ago |
marcelb | 0b5ed3f47b | 1 year ago |
@ -0,0 +1 @@ |
|||||||
|
*.o |
@ -1,6 +1,15 @@ |
|||||||
{ |
{ |
||||||
"files.associations": { |
"files.associations": { |
||||||
"string": "cpp", |
"string": "cpp", |
||||||
"vector": "cpp" |
"vector": "cpp", |
||||||
|
"*.tcc": "cpp", |
||||||
|
"deque": "cpp", |
||||||
|
"unordered_map": "cpp", |
||||||
|
"system_error": "cpp", |
||||||
|
"iostream": "cpp", |
||||||
|
"type_traits": "cpp", |
||||||
|
"tuple": "cpp", |
||||||
|
"array": "cpp", |
||||||
|
"utility": "cpp" |
||||||
} |
} |
||||||
} |
} |
Binary file not shown.
Binary file not shown.
@ -1,128 +0,0 @@ |
|||||||
#include "../lib/sql3.hpp" |
|
||||||
|
|
||||||
sql3::sql3(const string _path, bool _keepOpen) { |
|
||||||
path = _path; |
|
||||||
keepOpen = _keepOpen; |
|
||||||
|
|
||||||
if (keepOpen) { |
|
||||||
if (open()) { |
|
||||||
printf("[ERROR] Ne mogu otvoriti bazu podataka!"); |
|
||||||
exit(1); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
bool sql3::open() { |
|
||||||
uint operationStatus = sqlite3_open(path.c_str(), &db); |
|
||||||
return operationStatus != SQLITE_OK; |
|
||||||
} |
|
||||||
|
|
||||||
bool sql3::close() { |
|
||||||
uint operationStatus = sqlite3_close(db); |
|
||||||
db = NULL; |
|
||||||
return operationStatus != SQLITE_OK; |
|
||||||
} |
|
||||||
|
|
||||||
bool sql3::run(const string sql_command) { |
|
||||||
|
|
||||||
if (!keepOpen) { |
|
||||||
if (open()) { |
|
||||||
printf("[ERROR] Ne mogu otvoriti bazu podataka!"); |
|
||||||
exit(1); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
bool r = true; |
|
||||||
char *messaggeError; |
|
||||||
uint exec_stuts; |
|
||||||
exec_stuts = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); |
|
||||||
|
|
||||||
if (exec_stuts != SQLITE_OK) { |
|
||||||
// printf("[ERROR] Ne mogu čitati bazu podataka!");
|
|
||||||
sqlite3_free(messaggeError); |
|
||||||
r = false; |
|
||||||
} |
|
||||||
else { |
|
||||||
Answer = responseDatabase; |
|
||||||
} |
|
||||||
|
|
||||||
if (!keepOpen) { |
|
||||||
if(close()) { |
|
||||||
printf ("ERROR Zatvaranja baze podataka!"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return r; |
|
||||||
} |
|
||||||
|
|
||||||
string sql3::answer() { |
|
||||||
string _Answer = Answer; |
|
||||||
Answer.clear(); |
|
||||||
return _Answer; |
|
||||||
} |
|
||||||
|
|
||||||
string sql3::ask(const string sql_command) { |
|
||||||
if (!run(sql_command)) { |
|
||||||
printf("[ERROR] Ne mogu čitati bazu podataka!"); |
|
||||||
return {}; |
|
||||||
} |
|
||||||
else { |
|
||||||
return answer(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
map<string, vector<string>> sql3::query(const string sql_command) { |
|
||||||
if (!run(sql_command)) { |
|
||||||
printf("[ERROR] Ne mogu čitati bazu podataka!"); |
|
||||||
return {}; |
|
||||||
} |
|
||||||
else { |
|
||||||
mapit(); |
|
||||||
map<string, vector<string>> _parsed = parsed; |
|
||||||
parsed.clear(); |
|
||||||
return _parsed; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void sql3::mapit() { |
|
||||||
string a = Answer; |
|
||||||
|
|
||||||
while (a.find("\n") < a.length()) { |
|
||||||
uint lineend = a.find("\n"); |
|
||||||
string oneline = a.substr(0, lineend); |
|
||||||
a.erase(0, lineend+1); |
|
||||||
|
|
||||||
size_t sep = oneline.find(" = "); |
|
||||||
string key = oneline.substr(0, sep); |
|
||||||
string value = oneline.substr(sep + 3, oneline.length()); |
|
||||||
value = value.substr(0, value.length()); |
|
||||||
|
|
||||||
parsed[key].push_back(value); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
sql3::~sql3() { |
|
||||||
if(close()) { |
|
||||||
printf ("ERROR Zatvaranja baze podataka!"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Callback funkcija za sqlite3
|
|
||||||
*/ |
|
||||||
|
|
||||||
static int callback(void* data, int argc, char** argv, char** azColName) { |
|
||||||
int i; |
|
||||||
fprintf(stderr, "%s: ", (const char*)data); |
|
||||||
char res[1000]; |
|
||||||
for (i = 0; i < argc; i++) { |
|
||||||
sprintf(res, "%s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); |
|
||||||
responseDatabase += res; |
|
||||||
responseDatabase += '\n'; |
|
||||||
} |
|
||||||
return 0; |
|
||||||
} |
|
@ -0,0 +1,114 @@ |
|||||||
|
#include "../lib/sqlite3.hpp" |
||||||
|
|
||||||
|
namespace marcelb::SQlite3 { |
||||||
|
|
||||||
|
SQLite3::SQLite3(const string _path, bool _keepOpen) { |
||||||
|
path = _path; |
||||||
|
keepOpen = _keepOpen; |
||||||
|
|
||||||
|
if (keepOpen) { |
||||||
|
if (open()) { |
||||||
|
throw string("[ERROR] Unable to open database "); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool SQLite3::open() { |
||||||
|
uint operationStatus = sqlite3_open(path.c_str(), &db); |
||||||
|
return operationStatus != SQLITE_OK; |
||||||
|
} |
||||||
|
|
||||||
|
bool SQLite3::close() { |
||||||
|
uint operationStatus = sqlite3_close(db); |
||||||
|
db = NULL; |
||||||
|
return operationStatus != SQLITE_OK; |
||||||
|
} |
||||||
|
|
||||||
|
map<string, vector<string>> SQLite3::query(const string sql_command) { |
||||||
|
io.lock(); |
||||||
|
if (!keepOpen) { |
||||||
|
if (open()) { |
||||||
|
io.unlock(); |
||||||
|
throw string("[ERROR] Unable to open database "); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool r = true; |
||||||
|
char *messaggeError; |
||||||
|
uint exec_status; |
||||||
|
exec_status = sqlite3_exec(db, sql_command.c_str(), callback, NULL, &messaggeError); |
||||||
|
|
||||||
|
string result; |
||||||
|
if (exec_status != SQLITE_OK) { |
||||||
|
sqlite3_free(messaggeError); |
||||||
|
r = false; |
||||||
|
} |
||||||
|
else { |
||||||
|
result = responseDatabase; |
||||||
|
responseDatabase.clear(); |
||||||
|
} |
||||||
|
|
||||||
|
if (!keepOpen) { |
||||||
|
if(close()) { |
||||||
|
io.unlock(); |
||||||
|
throw string("[ERROR] Unable to close database "); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
map<string, vector<string>> _parsed; |
||||||
|
|
||||||
|
if (!r) { |
||||||
|
io.unlock(); |
||||||
|
throw string("[ERROR] Unable to read database "); |
||||||
|
} |
||||||
|
|
||||||
|
else { |
||||||
|
_parsed = parse(result); |
||||||
|
} |
||||||
|
io.unlock(); |
||||||
|
return _parsed; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
map<string, vector<string>> SQLite3::parse(const string& result) { |
||||||
|
string a = result; |
||||||
|
map<string, vector<string>> parsed; |
||||||
|
|
||||||
|
while (a.find("\n") < a.length()) { |
||||||
|
uint lineend = a.find("\n"); |
||||||
|
string oneline = a.substr(0, lineend); |
||||||
|
a.erase(0, lineend+1); |
||||||
|
|
||||||
|
size_t sep = oneline.find(" = "); |
||||||
|
string key = oneline.substr(0, sep); |
||||||
|
string value = oneline.substr(sep + 3, oneline.length()); |
||||||
|
value = value.substr(0, value.length()); |
||||||
|
parsed[key].push_back(value); |
||||||
|
} |
||||||
|
|
||||||
|
return parsed; |
||||||
|
} |
||||||
|
|
||||||
|
SQLite3::~SQLite3() { |
||||||
|
if(close()) { |
||||||
|
// throw string("[ERROR] Unable to close database ");
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback funkcija za SQLite3
|
||||||
|
*/ |
||||||
|
|
||||||
|
static int callback(void* data, int argc, char** argv, char** azColName) { |
||||||
|
int i; |
||||||
|
char res[1000]; |
||||||
|
for (i = 0; i < argc; i++) { |
||||||
|
sprintf(res, "%s = %s", azColName[i], argv[i] ? argv[i] : "NULL"); |
||||||
|
responseDatabase += string(res) + string("\n"); |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -1,19 +1,32 @@ |
|||||||
#include <iostream> |
#include <iostream> |
||||||
|
using namespace std; |
||||||
|
|
||||||
#include "../lib/sql3.hpp" |
|
||||||
|
|
||||||
using namespace std; |
#include "../lib/sqlite3.hpp" |
||||||
|
using namespace marcelb::SQlite3; |
||||||
|
|
||||||
int main() { |
int main() { |
||||||
|
try { |
||||||
|
|
||||||
sql3 mydb("../example/example.db", false); |
SQLite3 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"); |
||||||
|
// cout << endl << res["NAME"][1];
|
||||||
|
|
||||||
|
// cout << endl << res["MAIL"][1];
|
||||||
|
|
||||||
|
for (auto i : res) { |
||||||
|
for (auto j: i.second) { |
||||||
|
cout << i.first << " : " << j << endl;
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
cout << endl << res["NAME"][0]; |
} catch (const string err) { |
||||||
|
cout << err << endl; |
||||||
|
} |
||||||
|
|
||||||
return 0; |
return 0; |
||||||
} |
} |
Binary file not shown.
Loading…
Reference in new issue