3

我有一个这样的 sqlite3 表“地址”:

+----+------+--------+
| ID | name | number |
+----+------+--------+
| 1  | John | 413434 |
+----+------+--------+

我想填充全局向量,我可以在其他地方使用它并将它与其他一些数据连接起来。到目前为止,我有这个:

...
#include <sqlite3.h>

using namespace std;

static int callbackDB(void *NotUsed, int argc, char **argv, char **szColName)
{
    for(int i = 0; i < argc; i++)
        cout << szColName[i] << " = " << argv[i] << endl;

    return 0;
}

int main(int argc, char* argv[]) {
    vector<vector<string> > table;
    for( int i = 0; i < 2; i++ )
        table.push_back(std::vector< std::string >());


    sqlite3 *db;
    char *szErrMsg = 0;

    // open database
    int rc = sqlite3_open("database.db", &db);

    const char *query;
    query = "SELECT * FROM addresses";

    rc = sqlite3_exec(db, query, callbackDB, 0, &szErrMsg);

    return 0;
}

我怎样才能得到向量或其他方式的结果,以便我能够更轻松地使用它?

4

1 回答 1

7

的第四个参数sqlite3_exec可用于向回调传递信息。在您的情况下,指向的指针table会很有用:

typedef vector<vector<string> > table_type;
static int callbackDB(void *ptr, int argc, char* argv[], char* cols[])
{
    table_type* table = static_cast<table_type*>(ptr);
    vector<string> row;
    for (int i = 0; i < argc; i++)
        row.push_back(argv[i] ? argv[i] : "(NULL)");
    table->push_back(row);
    return 0;
}
...
    rc = sqlite3_exec(db, query, callbackDB, &table, &errmsg);

但是,使用这样的回调并不是很有用,因为您只会得到一堆裸字符串。您应该直接使用 prepare/step/finalize 接口,以便可以使用正确的数据类型:

class Address {
public:
    Address(int id, const string& name, const string& number);
    ...
}

    ...
    vector<Address> addresses;
    sqlite3_stmt *stmt;
    rc = sqlite3_prepare_v2(db, "SELECT id, name, number FROM addresses",
                            -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        cerr << "SELECT failed: " << sqlite3_errmsg(db) << endl;
        return ...; // or throw
    }
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
        int id = sqlite3_column_int(stmt, 0);
        const char* name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
        const char* number = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
        // let's assume number can be NULL:
        addresses.push_back(Address(id, name, number ? number : ""));
    }
    if (rc != SQLITE_DONE) {
        cerr << "SELECT failed: " << sqlite3_errmsg(db) << endl;
        // if you return/throw here, don't forget the finalize
    }
    sqlite3_finalize(stmt);
于 2013-04-06T08:38:39.117 回答