在 C++ 中存储和操作 SQLite 查询的结果

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

我是 C++ 和 SQLlite 的初学者,正在尝试编译可以操纵 SQLite 查询结果的代码。

我在将结果存储到 .txt 文件时遇到困难,这将使我能够操纵结果,但不知道从哪里开始,

因此,我只能在 MA.txt 中看到一行,但是我想存储所有结果,

我想将结果存储到 .txt 文件中,因为存储结果后,我必须将结果分成预定义的长度并找到最大值和最小值,并报告第一行和最后一行。

#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;
}

我更改了代码,但是在编译时出现 "os does not name a type" 错误,

我应该把offsting放在哪里,不好意思我真的是菜鸟:(

#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();
}

我认为这是因为您打开文件的方式。每次读取新记录时都会打开它,这会导致它每次都回到开头。

解决此问题的一种方法是在函数外部声明 ofstream 并在函数外部打开它并在最后关闭它。修复它的另一种方法是打开设置了 std::app 标志的 ofstream,这样它将附加到文件而不是重写它。

另一件事是您从回调函数中 returning '0'。您需要 return SQLITE_OK 来告诉它继续。

不确定这是否有效,因为我还没有测试过,但试试这个代码:

#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;
}