我的代码如下。我试图简单地从我已经创建的表中获取数据。如何将回调函数中定义的数组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;
}
答案 0 :(得分:9)
您需要将要存储数据的数组作为回调的第一个参数传递给sqlite3 docs
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
这是一个基于你的代码的设计实例,有些整理:
#include <vector>
#include <string>
#include <iostream>
#include "sqlite3.h"
using namespace std;
sqlite3* db;
using Record = std::vector<std::string>;
using Records = std::vector<Record>;
int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names)
{
Records* records = static_cast<Records*>(p_data);
try {
records->emplace_back(p_fields, p_fields + num_fields);
}
catch (...) {
// abort select on failure, don't let exception propogate thru sqlite3 call-stack
return 1;
}
return 0;
}
Records select_stmt(const char* stmt)
{
Records records;
char *errmsg;
int ret = sqlite3_exec(db, stmt, select_callback, &records, &errmsg);
if (ret != SQLITE_OK) {
std::cerr << "Error in select statement " << stmt << "[" << errmsg << "]\n";
}
else {
std::cerr << records.size() << " records returned.\n";
}
return records;
}
void sql_stmt(const char* stmt)
{
char *errmsg;
int ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
if (ret != SQLITE_OK) {
std::cerr << "Error in select statement " << stmt << "[" << errmsg << "]\n";
}
}
int main()
{
if (sqlite3_open("test.db", &db) != SQLITE_OK) {
std::cerr << "Could not open database.\n";
return 1;
}
Records records = select_stmt("SELECT * FROM test");
sqlite3_close(db);
for (auto& record : records) {
// do something with your records
}
return 0;
}
你的问题是标签c ++,所以我删除了你对标准c api调用的使用,转而使用标准c ++。
答案 1 :(得分:0)
@Goji谢谢您的出色回答。
只是一个更新,当数据库中有空值时,此程序将给出“ basic_string :: _ M_construct null无效”,因为我们在try块中使用nullptr调用构造函数,它将捕获异常。
为避免这种情况,您可以检查该值是否为null。
for(int i = 0; i<num_fields; i++){
printf("%s = %s\n", p_col_names[i], p_fields[i] ? p_fields[i] : "NULL");
if(p_fields[i] == nullptr){
p_fields[i] = new char(0);
}
}
干杯;)