我有如下代码。我试图简单地从我已经创建的表中获取数据。如何将回调函数中定义的数组 p_fields 中的数据放入 main 中的变量中。我可以在 main 中定义一个 char ** 数组并在回调函数中以某种方式将数据复制到其中吗?
非常感谢,希亚姆。
#include <string.h>
#include <stdio.h>
#include "sqlite3.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
sqlite3* db;
int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names) {
int i;
for(i=0; i < num_fields; i++) {
if (p_fields[i]) {
printf("%20s", p_fields[i]);
}
else {
printf("%20s", " ");
}
}
printf("\n");
return 0;
}
void select_stmt(const char* stmt) {
char *errmsg;
int ret;
int nrecs = 0;
float var;
ret = sqlite3_exec(db, stmt, select_callback, &nrecs, &errmsg);
if(ret!=SQLITE_OK) {
printf("Error in select statement %s [%s].\n", stmt, errmsg);
}
else {
printf("\n %d records returned.\n", nrecs);
}
cout<< ret << endl;
}
void sql_stmt(const char* stmt) {
char *errmsg;
int ret;
ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
if(ret != SQLITE_OK) {
printf("Error in statement: %s [%s].\n", stmt, errmsg);
}
}
int main() {
sqlite3_open("Flamelet.db", &db);
if(db == 0) {
printf("\nCould not open database.");
return 1;
}
sqlite3_stmt *stmt;
select_stmt("SELECT density from Ftable where PROG=10.0");
sqlite3_close(db);
return 0;
}