c++ - 如何使用 libpqxx 中的 pqxx::stateless_cursor 类?

标签 c++ postgresql libpqxx

我正在学习 libpqxx,PostgreSQL 的 C++ API。我想使用 pqxx::stateless_cursor 类,但是 1) 我发现 Doxygen 输出在这种情况下没有帮助,并且 2) pqxx.org 网站已经关闭一段时间了。

有人知道怎么用吗?

我相信这就是我构建一个的方式:

pqxx::stateless_cursor <pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", ?, ? );

最后两个参数称为 cnamehold,但未记录。

创建游标后,我将如何在 for() 循环中使用它一次获取每一行?

最佳答案

感谢@Eelke 对cnamehold 的评论。

我想出了如何使 pqxx::stateless_cursor 工作。我不知道是否有更清晰或更明显的方法,但这里有一个例子:

pqxx::work work( conn );
pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", "mycursor", false );

for ( size_t idx = 0; true; idx ++ )
{
    pqxx::result result = cursor.retrieve( idx, idx + 1 );
    if ( result.empty() )
    {
        // nothing left to read
        break;
    }

    // Do something with "result" which contains a single
    // row in this example since we told the cursor to
    // retrieve row #idx (inclusive) to idx+1 (exclusive).
    std::cout << result[ 0 ][ "name" ].as<std::string>() << std::endl;
}

关于c++ - 如何使用 libpqxx 中的 pqxx::stateless_cursor 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16128142/

相关文章:

c++ - 使用 pqxx (postgres) 的简单 C++ 程序

c++ - grpc 服务中连接的简单线程安全 vector

c++ - 任何运算符、函数调用和构造函数的通用持续时间计

c++ - 在头文件中声明多个函数

ruby-on-rails - 无法使用 where 子句 rails 检索信息

c# - 无法安装 Microsoft Power Bi Postgresql 连接器

c++ - Visual Studio 2008 x64 下的 libpqxx

c++不使用C标准库将字符串和int转换为char *

c++ - PC与Arduino串口通信

postgresql - fabric postgresql 创建数据库