From 3524911c1576b3e85153e99e964dccb7e6b530e8 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 13 Jun 2019 15:08:03 +0900 Subject: [PATCH 1/5] Fixed string and char compare. Changed query result to std::optional from c++17 --- Makefile | 2 +- sqliteclient.cpp | 20 ++++++++++---------- sqliteclient.hpp | 5 +++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 8d4ce21..842a396 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=c++ -CFLAGS=-std=c++11 -lsqlite3 +CFLAGS=-std=c++17 -lsqlite3 all: $(CC) $(CFLAGS) main.cpp -o sqlite diff --git a/sqliteclient.cpp b/sqliteclient.cpp index 619cd48..eddeef3 100644 --- a/sqliteclient.cpp +++ b/sqliteclient.cpp @@ -19,17 +19,18 @@ void SqliteClient::connect() this->connected=true; } -void SqliteClient::executeQuery(const char* query, std::vector &resVec) +std::optional> *SqliteClient::executeQuery(const char* query) { if(!this->connected) - return; + return nullptr; + auto *res=new std::optional>(); std::string queryType; uint8_t i=0; - while(query[i]!=*" ") + while(query[i]!=' ') queryType+=query[i++]; if(queryType=="SELECT") { - std::vector *selectRes=new std::vector(); + std::vector selectRes; sqlite3_stmt *stmt; sqlite3_prepare_v2(db, query, -1, &stmt, NULL); uint8_t colCount=sqlite3_column_count(stmt); @@ -38,22 +39,23 @@ void SqliteClient::executeQuery(const char* query, std::vector &res for(uint8_t i=0;ipush_back(colNames); + selectRes.push_back(colNames); while(sqlite3_column_text(stmt, 0)) { std::string str; for(uint8_t i=0;ipush_back(str); + selectRes.push_back(str); sqlite3_step(stmt); } - resVec=*selectRes; - delete selectRes; + res->emplace(selectRes); + return res; } else { sqlite3_exec(db, query, NULL, 0, 0); + return nullptr; } } @@ -66,8 +68,6 @@ void SqliteClient::closeConnection() SqliteClient::~SqliteClient() { -//просто для проверки на случай если сам забыл закрыть -//кстати интересно а что случается если не закрыть? this->closeConnection(); } diff --git a/sqliteclient.hpp b/sqliteclient.hpp index 12aca18..b4c9cd9 100644 --- a/sqliteclient.hpp +++ b/sqliteclient.hpp @@ -1,13 +1,14 @@ #include #include #include +#include class SqliteClient { public: SqliteClient(const char *fileName); void connect(); - void executeQuery(const char* query, std::vector &resVec); - void returnResult(std::vector &resVec); + //void executeQuery(const char* query, std::vector &resVec); + std::optional> *executeQuery(const char* query); void closeConnection(); ~SqliteClient(); private: From 382adac0ccc96026aeab69c42fd93079616ce912 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 13 Jun 2019 15:16:28 +0900 Subject: [PATCH 2/5] Create README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8842c44 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# sqlitecpp +lightweight Sqlite wrapper on c++ +Query result returns as *std::optional>. Columns splited by "|" symbol. Need a sqlite3 libraries for compile From 0993b971b9ff2e63f3a14646ece3fd39a619e8b1 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 13 Jun 2019 15:17:04 +0900 Subject: [PATCH 3/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8842c44..3bbf57f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # sqlitecpp -lightweight Sqlite wrapper on c++ +Lightweight Sqlite wrapper on c++\n Query result returns as *std::optional>. Columns splited by "|" symbol. Need a sqlite3 libraries for compile From 8a89b701f1c408aba7f4439c63d711ae0bd01c5f Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 13 Jun 2019 15:17:35 +0900 Subject: [PATCH 4/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3bbf57f..344ae86 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # sqlitecpp -Lightweight Sqlite wrapper on c++\n +Lightweight Sqlite wrapper on c++
Query result returns as *std::optional>. Columns splited by "|" symbol. Need a sqlite3 libraries for compile From dc3db887220859f27277da206c3dd842d98be988 Mon Sep 17 00:00:00 2001 From: Loginov Vladimir Date: Fri, 2 May 2025 19:20:51 +0900 Subject: [PATCH 5/5] change compiler option to c++20 query result was moved from return value to executeQuery() method params query result was changed into vector of unordered maps where each element of vector represents a row and map elements represents the row cells with col. name as map key --- Makefile | 2 +- sqliteclient.cpp | 30 ++++++++++++++++++++++++++++++ sqliteclient.hpp | 9 +++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 842a396..b5cff6c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=c++ -CFLAGS=-std=c++17 -lsqlite3 +CFLAGS=-std=c++20 -lsqlite3 all: $(CC) $(CFLAGS) main.cpp -o sqlite diff --git a/sqliteclient.cpp b/sqliteclient.cpp index eddeef3..ab900cf 100644 --- a/sqliteclient.cpp +++ b/sqliteclient.cpp @@ -1,3 +1,4 @@ +#include #include "sqliteclient.hpp" SqliteClient::SqliteClient(const char *fileName) @@ -59,6 +60,35 @@ std::optional> *SqliteClient::executeQuery(const char* } } +void SqliteClient::executeQuery(const char *query, std::vector> *queryResult) +{ + if(!connected) + return; + + uint8_t i=0; + std::string queryType=""; + size_t querySize=strlen(query); + while(query[i]!=' '&&i row; + for(uint8_t i=0;i #include +#include #include #include + class SqliteClient { public: @@ -9,6 +14,8 @@ class SqliteClient void connect(); //void executeQuery(const char* query, std::vector &resVec); std::optional> *executeQuery(const char* query); + + void executeQuery(const char *query, std::vector> *queryResult); void closeConnection(); ~SqliteClient(); private: @@ -16,3 +23,5 @@ class SqliteClient const char* fileName; bool connected=false; }; + +#endif