/* * dtata.cpp * Copyright (C) 2023 Leonardo de Araújo Lima * * dstorage is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * dstorage is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "sdata.h" #include #include #include #include #include #include #include using namespace std; // sdata::sdata(){ cout << "class constructor >>" << endl; }; // int sdata::add_data( const char *n , const char* d) { if (debug == 1) { cout << "índice de dados : " << n << " dados/infomações : " << d << endl; } iname.push_back(n); idata.push_back(d); nitens++; cout << "número de dados: " << nitens << endl; return 0; }; // int sdata::citens(){ cout << "número de índices armazenados: " << iname.size() << endl; return 0; }; // int sdata::show() { cout << "Informações." << endl; for (int i=0; i < (nitens ); i++) { if (debug == 1) { cout << "informações sobre o membros de dados: " << i << endl; }; cout << iname.at(i) << " : " << idata.at(i) << endl; }; cout << "### --- ###" << endl; return 0; }; // void sdata::set_db(const char *name) { dbn = name; }; // void sdata::set_tb(const char *name) { tbn = name; }; // void sdata::set_dpath(const char *path) { dpath = path; }; // int sdata::dbinfo() { cout << "informações sobre o banco de dados. \n" << "#----#" << endl; cout << "Path: " << dpath << endl; cout << "Banco de dados: " << dbn << endl; cout << "Tabela: " < 0 printf("The data in column \"%s\" is: %s\n", columns[idx], data[idx]); } printf("\n"); return 0; } // void sdata::dbtest() { // sqlite3* db; int res = sqlite3_open(dbn, &db); if(res) //database failed to open cout << "Database verification failed!" << endl; else { cout << "database verified." << endl; //your database code here } sqlite3_close(db); } sdata::~sdata(){ cout << "class destructor <<" << endl; iname.clear(); idata.clear(); }; // int sdata::cDBase() { string sql = "CREATE TABLE " + string(tbn) + "("; sql.append("id int PRIMARY KEY"); for (int i=0; i < (nitens ); i++) { cout << iname.at(i) << " : " << idata.at(i) << endl; sql.append(", " + iname[i] + " CHAR(254)"); }; sql.append(")"); cout <<"creating database and table!" << endl; if (debug == 1){ cout << "sql statement: " << sql << endl; } int rc; rc = sqlite3_open(dbn, &db); if( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return 0; } else { fprintf(stderr, "Opened database successfully\n"); } rc = sqlite3_exec(db, sql.c_str(), callback, (void*)data, &zErrMsg); if( rc != SQLITE_OK ) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Operation done successfully\n"); } sqlite3_close(db); return 0; }; // bool sdata::addreg() { cnreg(); string sql = "INSERT INTO " + string(tbn) + " VALUES (" + to_string(nreg); for (int i=0; i < (nitens ); i++) { cout << iname.at(i) << ": " << idata.at(i) << endl; sql.append(", '" + idata[i] + "'"); }; sql.append(")"); cout <<"data to store on database!" << endl; if (debug == 1){ cout << "sql statement used: " << sql << endl; } int rc; rc = sqlite3_open(dbn, &db); if( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return 0; } else { fprintf(stderr, "\n"); } rc = sqlite3_exec(db, sql.c_str(), callback, (void*)data, &zErrMsg); if( rc != SQLITE_OK ) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "database peration done successfully\n"); return true; } sqlite3_close(db); return true; } // int sdata::cnreg(){ sqlite3_stmt *stmt; int nid; string sqldata = "SELECT id FROM " + string(tbn); const char *sql = sqldata.c_str(); cout << "nreg sql :" << sql << endl; int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { nid = sqlite3_column_int(stmt, 1); // ... } if (rc != SQLITE_DONE) { cout << nreg << "verifying nreg" <