注意: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
函数中。
答案 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;
}