C++/SQLite 只输出一行数据 [英] C++/SQLite only outputting one row of data

查看:52
本文介绍了C++/SQLite 只输出一行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我的 5 个测试行中只有一个将在 C++ 中输出.我的代码是:

I'm having an issue where only one of my 5 test rows will output in C++. My code is:

#include <cstdio>
#include <sqlite3.h>
#include <windows.h>
#include <wincrypt.h>
#include <string>
#include <vector>
#include <iostream>

using namespace std;
/*Definitions*/
sqlite3 *db;
void *arg;
char *err;
const char* stmt = "SELECT * from table";
/*End of Definitions*/

int exec(void *arg, int argc, char **argv, char **column) {
    int i;
    for(i = 0; i < argc; i++) {
        cout << column[i] << ": " << argv[i] << endl;
    }
    cout << "------" << endl;
}

int main() {
    int rc = sqlite3_open("test.sqlite", &db); /*Open db "test.sqlite"*/
    if(!rc) {
        while(true) {
            sqlite3_exec(db, stmt, exec, arg, &err);
            if(err) {
                break;
            }
        }
    }
    /*Ending Stuffz (NOTHING BEYOND THIS POINT)*/
    cin.get();
    return 0;
}

我没有收到任何错误;它纯粹只是输出第一行.感谢任何帮助,谢谢.

I am not getting any errors; it is purely just outputting the first row. Any help is appreciated, thanks.

推荐答案

exec 函数最后只返回 0

Just return 0 in the end of exec function

说明:

在函数exec"中,当它什么都不返回时,数据库服务器的行为就像函数被某种方式终止(就像某些东西破坏了函数并产生了错误)所以它只是停止发送数据,因为回调函数因错误而终止.... 所以当我们返回 0 时,我们说函数exec"工作正常,所以它继续发送数据......如果你返回任何其他整数,它会像函数因错误而终止并停止发送一样处理它数据.我遇到了同样的问题,我通过使回调函数 (exec) 返回 0 来解决它.

In function 'exec" when it return nothing, the database server acts like the function is terminated by somehow (like something corrupt the function and made an error) so it just stop sending data because the call back function terminated with an error.... so when we return 0 we say that the function "exec" is working normally so it keeps sending data ... and if you return any other integer it will deal with it like the function terminated by an error and stop sending data. I faced the same issue and I solved it by making the callback function (exec) return 0.

这篇关于C++/SQLite 只输出一行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆