PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come usare la classe pqxx::stateless_cursor da libpqxx?

Grazie @Eelke per i commenti su cname e hold .

Ho capito come far funzionare pqxx::stateless_cursor. Non ho idea se esiste un modo più pulito o più ovvio, ma ecco un esempio:

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