nily
nily

Reputation: 67

Storing and manipulating the results of SQLite query in C++

I am a beginner to C++ and SQLlite and trying to compile code which can manipulate the results of a query from SQLite.

I am having difficulties in storing the results to a .txt file, which will enable me to manipulate results, and don't know where to start,

As a result, I can only see one row in MA.txt however I would like to store all of the results,

I want to store results into a .txt file because after I store the results, I have to divide the results into predefined lengths and find max and min values and report the first row and last row as well.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <sqlite3.h> 
#include <string> 
#include <locale> 
#include <sstream> 
#include <time.h>
#include <stdio.h>
#include <fstream> 
#include <vector>

using namespace std;

static int callback2(void *data, int argc, char **argv, char **azColName)
{
    ofstream os;
    os.open("MA.txt");
    os << argv[0] << endl;
    return 0;
    os.close();
}

int main(int argc, char* argv [])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    const char* data = "Callback function called";
    rc = sqlite3_open("test.db", &db);
    if (rc){
        cout << "Can't open database: %s\n" << sqlite3_errmsg(db);
        exit(0);
    }
    string sql = "SELECT * from forex;";
    rc = sqlite3_exec(db, sql.c_str(), callback2, (void*) data, &zErrMsg);
    if (rc != SQLITE_OK){
        cout << "SQL error:" << zErrMsg;
        sqlite3_free(zErrMsg);
    }

    return 0;
}

I changed the code however I got "os does not name a type" error while compiling,

where should I put the offsting, sorry I am really a noobie:(

#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <sqlite3.h> 
#include <string> 
#include <locale> 
#include <sstream> 
#include <time.h>
#include <stdio.h>
#include <fstream> 
#include <vector>

using namespace std;
ofstream os;
os.open("MA.txt");
static int callback2(void *data, int argc, char **argv, char **azColName)
{
    os << argv[0] << endl;
    return 0;
}

int main(int argc, char* argv [])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    const char* data = "Callback function called";
    rc = sqlite3_open("test.db", &db);
    if (rc){
        cout << "Can't open database: %s\n" << sqlite3_errmsg(db);
        exit(0);
    }
    string sql = "SELECT * from forex;";
    rc = sqlite3_exec(db, sql.c_str(), callback2, (void*) data, &zErrMsg);
    if (rc != SQLITE_OK){
        cout << "SQL error:" << zErrMsg;
        sqlite3_free(zErrMsg);
    }

    return 0;
    os.close();
}

Upvotes: 1

Views: 2002

Answers (1)

Cute
Cute

Reputation: 36

I think that this is because of the way you are opening the file. You are opening it every time you read a new record and this causes it to go back to the beginning every time.

One way to fix this is to declare the ofstream outside the function and open it outside the function and close it at the very end. Another way to fix it is to open the ofstream with the std::app flag set, so that it will append to the file instead of rewriting it.

Another thing is that you are returning '0' from the call back function. You need to return SQLITE_OK to tell it to continue.

Not sure if this will work because I haven't tested it, but try this code:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <sqlite3.h> 
#include <string> 
#include <locale> 
#include <sstream> 
#include <time.h>
#include <stdio.h>
#include <fstream> 
#include <vector>

using namespace std;
static int callback2(void *data, int argc, char **argv, char **azColName) {
    ofstream os("MA.txt", ios::app);
    os << argv[0] << endl;
    os.close();
    return SQLITE_OK;
}

int main(int argc, char* argv []) {
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    const char* data = "Callback function called";
    rc = sqlite3_open("test.db", &db);
    if (rc){
        cout << "Can't open database: %s\n" << sqlite3_errmsg(db);
        exit(0);
    }
    char sql[21] = "SELECT * from forex;";
    rc = sqlite3_exec(db, sql.c_str(), callback2, NULL, &zErrMsg);
    if (rc != SQLITE_OK){
        cout << "SQL error:" << zErrMsg;
        sqlite3_free(zErrMsg);
    }

    return 0;
}

Upvotes: 2

Related Questions