dev_old
marcelb 1 year ago
commit 4c171a5c99
  1. 6
      .vscode/settings.json
  2. BIN
      example/example copy.db
  3. BIN
      example/example.db
  4. 44
      lib/sql3.hpp
  5. 113
      src/sql3.cpp
  6. 1
      test/compile.sh
  7. 19
      test/test.cpp
  8. BIN
      test/test.o

@ -0,0 +1,6 @@
{
"files.associations": {
"string": "cpp",
"vector": "cpp"
}
}

Binary file not shown.

Binary file not shown.

@ -0,0 +1,44 @@
#ifndef _SQL3_
#define _SQL3_
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
// #include <algorithm>
#include <iostream>
using namespace std;
static string responseDatabase;
static int callback(void* data, int argc, char** argv, char** azColName);
class sql3 {
public:
sqlite3* db;
string path;
bool keepOpen;
// map<string, vector<string>> model;
string Answer;
map<string, vector<string>> parsed;
sql3(const string path, bool _keepOpen = false);
bool open();
bool close();
bool run(const string sql_command);
string answer();
void mapit();
string ask(const string sql_command);
map<string, vector<string>> query(const string sql_command);
~sql3();
};
#endif

@ -0,0 +1,113 @@
#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!");
}
}
}
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) {
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;
}
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 @@
g++ test.cpp ../src/* -o test.o -l sqlite3

@ -0,0 +1,19 @@
#include <iostream>
#include "../lib/sql3.hpp"
using namespace std;
int main() {
sql3 mydb("../example/example.db", true);
//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");
cout << endl << res["NAME"][0];
return 0;
}

Binary file not shown.
Loading…
Cancel
Save