Sqlite3:将数据库表名存储在向量中

时间:2020-06-10 03:48:46

标签: c++ database sqlite

注意:c ++ 98

我正在尝试在数据库中检索表名,并将它们存储在向量中作为字符串。

我的代码:

typedef vector<string> dbTableTypes;

static int callback1(void *handle, int argc, char **argv, char **azColName)
{
    // FILE *f1 = (FILE *)handle;
    dbTableTypes *dbTables = (dbTableTypes*)handle;
    int i;
    char *pEnd;
    const char *sep = "\t";

    for (i = 0; i < argc; i++)
    {
        cout << argv[i] << endl;
    }

    return 0;
}

int Database::dbTableNames()
{
    static string sqlGetTableNames = "select name from sqlite_master where type = 'table';";
    vector<string> dbTables;
    char* err_msg(NULL);
    int rc(SQLITE_ERROR);
    rc = sqlite3_exec(db, sqlGetTableNames.c_str(), callback1, &dbTables, &err_msg);
}

我可以在回调中打印表名,但是它没有反映在dbTableNames函数中。

1 个答案:

答案 0 :(得分:0)

我自己解决了。

好奇的解决方案:

typedef vector<string> dbTableTypes;

static int callback1(void *handle, int argc, char **argv, char **azColName)
{
    dbTableTypes *dbTables = (dbTableTypes*)handle;
    int i;
    char *pEnd;
    const char *sep = "\t";

    for (i = 0; i < argc; i++)
    {
        cout << argv[i] << endl;
        dbTables->push_back(argv[i]);
    }

    return 0;
}

int Database::dbTableNames()
{
    static string sqlGetTableNames = "select name from sqlite_master where type = 'table';";
    vector<string> dbTables;
    char* err_msg(NULL);
    int rc(SQLITE_ERROR);
    rc = sqlite3_exec(db, sqlGetTableNames.c_str(), callback1, &dbTables, &err_msg);

    for(int i = 0; i < dbTables.size(); i++)
    {
        cout << "Tables: " << dbTables[i] << endl;
    }
    return 0;
}
相关问题