【问题标题】:SQLite C++ query rows - how to handle multiple columnsSQLite C++ 查询行 - 如何处理多列
【发布时间】:2014-04-19 23:25:36
【问题描述】:

我是 SQLite、C++ 的新手,在理解如何处理从查询返回的多行时遇到了一些麻烦。我需要将每列的值放入一个变量中,以便以后处理。

到目前为止:

sqlite3_stmt* statement;

if (sqlite3_prepare_v2(getDatabase(), _sql.c_str(), -1, &statement, 0)
            == SQLITE_OK)
{
    int cols = sqlite3_column_count(statement);
    int result = 0;

    while(true)
    {
        result = sqlite3_step(statement);

        if(result == SQLITE_ROW)
        {
             for(int col = 0; col < cols; col++)
             {
                // go through each column and store it       
             }
        }
        else
        {
            break;
        }
    }
}

那么我在for 循环中该怎么做?

if (col == 0) // first column position
{
   // store it...
}
else if (col == 1) // second column position
{
    // store it...
}

或者有没有办法通过列名来做到这一点?

或者有没有更好的方法来解决这个问题?

【问题讨论】:

  • 最终目标是什么?循环完成后,您希望数据到底在哪里结束,接下来您想用它做什么?回复:列名 - 见sqlite3_column_name

标签: c++ sql database sqlite c++11


【解决方案1】:

我想通了。

正确的过程是遍历每一行并使用sqlite_column(statement, [col#])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多