gpt4 book ai didi

c++ - 在 C++ 中检索 Sqlite 表数据

转载 作者:太空狗 更新时间:2023-10-29 23:45:28 27 4
gpt4 key购买 nike

我有如下代码。我试图简单地从我已经创建的表中获取数据。如何将回调函数中定义的数组 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;
}

最佳答案

您需要将要存储数据的数组作为第一个参数传递给回调,如 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++。

关于c++ - 在 C++ 中检索 Sqlite 表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18839799/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com