简体   繁体   English

从sqlite检索数据并将其存储到数组c/c++

[英]Retrieve and store data from sqlite to array c/c++

I have a column of integers(containing port numbers) in my sqlite data base.我的 sqlite 数据库中有一个整数列(包含端口号)。 Now I want to retrieve list of ports from sqlite in an array form in a function and send data to those port numbers.现在我想在函数中以数组形式从 sqlite 检索端口列表,并将数据发送到这些端口号。 I tried to make a struct having pointer to Vector but it didn't work out.我试图创建一个指向 Vector 的结构,但没有成功。 Making char pointers caused Segmentation error.使字符指针导致分段错误。 Here is my code:这是我的代码:

struct ConnIDs{
    vector<int*> *ids;
};
static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
    ConnIDs *first=(ConnIDs *)NotUsed;

    for(int i = 0; i < argc; i++)
    {
        if (strcmp(szColName[i], "ConnID") == 0)
        {
            first->ids->push_back(argv[i]);
        }
        std::cout << szColName[i] << " = " << argv[i] << std::endl;
    }
    std::cout << "\n";
    return 0;
}

int main(){
    int result;
    sqlite3 *db;
    ConnIDs first;

    int count = 0; 
    char *szErrMsg = 0;
    int rc = sqlite3_open("Sqlite_Test.db", &db);
    if(rc)
    {
        std::cout << "Can't open database\n";
    } else
    {
        std::cout << "Open database successfully\n";
    }
    char *pSQL2[1];  
    pSQL2[0] = "Select * from Subscribers";
    rc = sqlite3_exec(db, pSQL2[0], callback, &first, &szErrMsg);
    if(rc != SQLITE_OK)
    {
        std::cout << "SQL Error: " << szErrMsg << std::endl;
        sqlite3_free(szErrMsg);
        break;
    }
    else
    {
        printf("count: %s\n", first.ids);
    }
}

This isn't an error regarding sqlite at all, it's just an uninitialised pointer.这根本不是关于 sqlite 的错误,它只是一个未初始化的指针。 Let's look at the relevant code:我们看一下相关代码:

struct ConnIDs{
    vector<int*> *ids;
};
...

ConnIDs first;
rc = sqlite3_exec(db, pSQL2[0], callback, &first, &szErrMsg);
...
static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
    ConnIDs *first=(ConnIDs *)NotUsed;
    for(int i = 0; i < argc; i++)
       if (strcmp(szColName[i], "ConnID") == 0)
           first->ids->push_back(argv[i]);
}

I've omitted unnecessary parts.我省略了不必要的部分。 first is an object of ConnIDs , which is a std::vector<int*>* - it's a pointer to a vector. firstConnIDs的对象,它是一个std::vector<int*>* - 它是一个指向向量的指针。 You pass first into your callback , then immediately try to push into ids ;first传入callback ,然后立即尝试推入ids however, you haven't initialised ids , so it's just pointing to rubbish.但是,您还没有初始化ids ,所以它只是指向垃圾。

The simplest fix is to just not make it a std::vector<int*>* and instead just make it std::vector<int*> , although I'm not certain int* is the correct type here, it will really depend on the data in your database.最简单的解决方法是不让它成为std::vector<int*>* ,而是让它成为std::vector<int*> ,虽然我不确定int*是这里的正确类型,但它真的会取决于数据库中的数据。 I would imagine either just int or std::string may be more applicable, but I'll leave that to you:我想要么int要么std::string可能更适用,但我会把它留给你:

With the above change, your code will work as-is but you'll need to change first->ids->push_back to first->ids.push_back通过上述更改,您的代码将按原样工作,但您需要将first->ids->push_back更改为first->ids.push_back


Additional notes:补充说明:

char *pSQL2[1];  
pSQL2[0] = "Select * from Subscribers";
rc = sqlite3_exec(db, pSQL2[0], callback, &first, &szErrMsg);

This is kinda weird.这有点奇怪。 I'd probably either just write rc = sqlite3_exec(db, "Select * from Subscribers", ...) or我可能要么只写rc = sqlite3_exec(db, "Select * from Subscribers", ...)要么

auto query = "Select * from Subscribers";
rc = sqlite3_exec(db, query.c_str(), callback, &first, &szErrMsg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM