Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // example of putting a dataset into SQL Lite this might have been parsed from a string
- // to the server and contains the products costs and quantities
- //
- // for SQL Lite you will need :-
- // sudo apt update
- // sudo apt-cache search sqlite
- // sudo apt install sqlite3
- //
- // To Compile as well as the header file here
- // https://github.com/dmitigr/sqlixx
- // you also need
- // sudo apt-get install libsqlite3-dev
- //
- // to compile
- // g++-7 -std=c++17 batch_request.cpp -o batch_request -lsqlite3
- //
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <optional>
- #include "sqlixx.hpp"
- #include <fstream>
- #include <algorithm>
- #include <map>
- using namespace std;
- int g_index {0};
- struct product_word_t {
- unsigned char ourText[100];
- unsigned char ourNums[20];
- unsigned char ourMoneys[20];
- };
- // parses string input to an int or null
- std::optional<double> ParseStringToDouble(string& arg)
- {
- try
- {
- return { std::stod(arg,0) }; // converts first number
- }
- catch (...)
- {
- std::cout << "cannot convert \'" << arg << "\' to int!\n";
- }
- return { };
- }
- int main()
- {
- namespace sqlixx = dmitigr::sqlixx;
- sqlixx::Connection c{"", SQLITE_OPEN_READWRITE | SQLITE_OPEN_MEMORY};
- product_word_t inputCatalog;
- std::string printitdesc;
- std::string printitmoneys;
- std::string printitqty;
- std::string costConvertTofloat;
- std::string qtyConvertTofloat;
- // ============= Create the table for products costs and quantity =======================
- c.execute(
- R"(
- create table if not exists prod_cost_tab(
- id integer primary key AUTOINCREMENT,
- cost real,
- qty real,
- ct text,
- printmoney text,
- printqty text,
- cb blob)
- )"
- );
- // =============== Truncate the product cost table. ======================================
- c.execute("delete from prod_cost_tab");
- // =============== set up the message object with dummy data ============================
- strcpy((char*)&inputCatalog.ourText[0],"This is an example product");
- strcpy((char*)&inputCatalog.ourMoneys[0],"$10.75");
- strcpy((char*)&inputCatalog.ourNums[0]," 5.6Kg");
- // =============== get the data from the message object =================================
- char * pout = (char*) &inputCatalog.ourText[0];
- printitdesc.append(pout);
- pout = (char*) &inputCatalog.ourMoneys[0];
- printitmoneys.append(pout);
- costConvertTofloat.append(pout+1); // append the number without the leading currency identifier
- auto cost_float = ParseStringToDouble(costConvertTofloat);
- pout = (char*) &inputCatalog.ourNums[0];
- printitqty.append(pout);
- qtyConvertTofloat.append(pout); // append the number can have e.g kg after but no leading char
- auto qty_float = ParseStringToDouble(qtyConvertTofloat);
- // =========== Populate the table with the information passed from the structure ==========
- if ((qty_float) && (cost_float)) // qty and cost are valid
- {
- auto s = c.prepare("insert into prod_cost_tab(id, cost, qty, ct, printmoney, printqty, cb) values(?, ?, ?, ?, ?, ?, ?)");
- c.execute("begin");
- s.execute(g_index, *cost_float, *qty_float, printitdesc, printitmoneys, printitqty, sqlixx::Blob{"four", 4});
- c.execute("end");
- g_index++;
- }
- // ============== Query the product cost table. ===========================================
- c.execute([](const sqlixx::Statement& s)
- {
- const auto b = s.result<sqlixx::Blob>("cb");
- const std::string_view cb{static_cast<const char*>(b.data()), b.size()};
- const auto t1 = s.result<sqlixx::Text_utf8>("ct");
- const auto t2 = s.result<std::string>("ct");
- const auto t3 = s.result<std::string_view>("ct");
- assert(!std::strcmp(t1.data(), t2.data()) && (t2 == t3));
- std::cout << "id: " << s.result<int>("id") << "\n"
- << "number cost: " << s.result<double>("cost") << "\n"
- << "quantity: " << s.result<double>("qty") << "\n"
- << "description: " << t3 << "\n"
- << "blob: " << cb << "\n";
- const auto t4 = s.result<sqlixx::Text_utf8>("printmoney");
- const auto t5 = s.result<std::string>("printmoney");
- const auto t6 = s.result<std::string_view>("printmoney");
- assert(!std::strcmp(t4.data(), t5.data()) && (t5 == t6));
- const auto t7 = s.result<sqlixx::Text_utf8>("printqty");
- const auto t8 = s.result<std::string>("printqty");
- const auto t9 = s.result<std::string_view>("printqty");
- assert(!std::strcmp(t7.data(), t8.data()) && (t8 == t9));
- //std::cout << t1 << " " << t4 << " " << t7 << std::endl;
- },
- "select * from prod_cost_tab where id >= ? and id < ?", 0, g_index);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement