首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在libpqxx中使用pqxx::stateless_cursor类?

如何在libpqxx中使用pqxx::stateless_cursor类?
EN

Stack Overflow用户
提问于 2013-04-21 13:02:52
回答 3查看 1.7K关注 0票数 4

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

有人知道怎么用吗?

我相信这就是我如何构建一个:

代码语言:javascript
复制
pqxx::stateless_cursor <pqxx::cursor_base::read_only, pqxx::cursor_base::owned>
    cursor( work, "SELECT * FROM mytable", ?, ? );

最后两个参数分别称为cnamehold,但没有文档记录。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-22 06:07:32

感谢@Eelke在cnamehold上的评论。

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

代码语言:javascript
复制
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;
}
票数 4
EN

Stack Overflow用户

发布于 2013-04-21 14:11:39

我不知道pqxx库,但基于postgresql的底层DECLARE命令,我猜

cname是游标的名称,因此它可以是postgresql通常接受的任何游标名称。

该保留指的是来自文档的游标的WITH hold选项:

WITH HOLD指定在创建游标的事务成功提交后可以继续使用该游标。WITHOUT指定不能在创建游标的事务之外使用游标。如果既未指定WITH HOLD也未指定WITH HOLD,则缺省设置为WITH HOLD。

票数 2
EN

Stack Overflow用户

发布于 2020-02-18 06:25:08

下面是另一个使用do-while()循环的游标示例:

代码语言:javascript
复制
     const std::conStr("user=" + opt::dbUser + " password=" + opt::dbPasswd + " host=" + opt::dbHost + " dbname=" + opt::dbName);                                            

      pqxx::connection conn(connStr);
      pqxx::work txn(conn);
      std::string selectString = "SELECT id, name FROM table_name WHERE condition";

      pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned> 
      cursor(txn, selectString, "myCursor", false);

      //cursor variables
      size_t idx = 0;       //starting location
      size_t step = 10000;  //number of rows for each chunk
      pqxx::result result;
      do{
        //get next cursor chunk and update the index
        result = cursor.retrieve( idx, idx + step );
        idx += step;

        size_t records = result.size();
        cout << idx << ": records pulled = " << records << endl;

        for( pqxx::result::const_iterator row : result ){
          //iterate over cursor rows
        }
      }
      while( result.size() == step ); //if the result.size() != step, we're on our last loop
      cout << "Done!" << endl;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16128142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档