如何使用pqxx :: stateless_cursor类从libpqxx? [英] How to use pqxx::stateless_cursor class from libpqxx?

查看:558
本文介绍了如何使用pqxx :: stateless_cursor类从libpqxx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习libpqxx,C ++ API到PostgreSQL。我想使用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,?,?);

最后两个parms称为 cname 保留,但未记录。



一旦创建光标,我将如何使用它

感谢@Eelke对<$ c的评论$ c> cname 和 hold



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

  pqxx ::工作); 
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())
{
//没有剩下的东西可以读取
break;
}

//在这个例子中使用result执行一个包含单个
//行的操作,因为我们告诉游标到
//检索行# idx(包含)到idx + 1(不包含)。
std :: cout<< result [0] [name] .as< std :: string>()< std :: endl;
}


I'm learning libpqxx, the C++ API to PostgreSQL. I'd like to use the pqxx::stateless_cursor class, but 1) I find the Doxygen output unhelpful in this case, and 2) the pqxx.org website has been down for some time now.

Anyone know how to use it?

I believe this is how I construct one:

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

The last two parms are called cname and hold, but are not documented.

And once the cursor is created, how would I go about using it in a for() loop to get each row, one at a time?

解决方案

Thanks @Eelke for the comments on cname and hold.

I figured out how to make pqxx::stateless_cursor work. I have no idea if there is a cleaner or more obvious way but here is an example:

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;
}

这篇关于如何使用pqxx :: stateless_cursor类从libpqxx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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