# cursor

> ## Directory

> > eXtremeDB_rt/samples/cursor

> ## Description

> > demonstrates basic eXtremeDB/rt cursor operations

> ## Used API

> > mco_cursor_first(), mco_cursor_next (), mco_cursor_last(), mco_cursor_prev(), mco_cursor_close(), Classname_index_cursor()

> ## Code snippet

> >         /* Navigate forward from first Record */
> >             printf( "\n\n\tFirst %d records:", nRecs );

> >             /* Open a read-only transaction */
> >             rc = mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t );
> >             if ( MCO_S_OK == rc ) {
> >               /* Open the cursor */
> >               rc = Record_autoid_index_cursor( t, &csr );
> >               if ( MCO_S_OK == rc )  {
> >                 /* Get first record */
> >                 rc = mco_cursor_first( t, &csr );

> >                 /* Show first nRecs */
> >                 for ( i = 0; i < nRecs && MCO_S_OK == rc; i++ ) {
> >                   /* Get Record from cursor */
> >                   rc = Record_from_cursor( t, &csr, &rec );
> >                   if ( MCO_S_OK == rc )  {
> >                     Record_key_get( &rec, &key );
> >                     Record_name_get( &rec, name, (mco_uint2)sizeof(name), &size );
> >                     printf( "\n\t%d: %s", key, name );
> >                     /* Move to next item */
> >                     rc = mco_cursor_next( t, &csr );
> >                   }
> >                 }
> >                 printf ("\n\tCursor done, rc=%d\n", rc);
> >                 /* Close cursor */
> >                 mco_cursor_close( t, &csr );
> >               }
> >               /*  Close the transaction */
> >               mco_trans_rollback( t );
> >             }

> >         /* Navigate backwards from last Record */
> >             printf( "\n\n\tLast %d records:", nRecs );

> >             /* Open a transaction */
> >             rc = mco_trans_start( db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t );
> >             if ( MCO_S_OK == rc )  {
> >               /* Open the cursor */
> >               rc = Record_autoid_index_cursor( t, &csr );
> >               if ( MCO_S_OK ==  rc )  {
> >                 /* Move to last item */
> >                 rc = mco_cursor_last( t, &csr );
> >                 /* Show last nRecs */
> >                 for ( i = 0; i < nRecs && MCO_S_OK == rc; i++ )  {
> >                   /* Get Record from cursor */
> >                   rc = Record_from_cursor( t, &csr, &rec );
> >                   if ( MCO_S_OK == rc )  {
> >                     Record_key_get( &rec, &key );
> >                     Record_name_get( &rec, name, (mco_uint2)sizeof(name), &size );
> >                     printf( "\n\t%d: %s", key, name );
> >                   }
> >                   /* Move to prev item */
> >                   rc = mco_cursor_prev( t, &csr );
> >                 }
> >      /* Close cursor */
> >                 mco_cursor_close( t, &csr );

> ## What to expect as an output

> >     	First 5 records:
> >     	0: Record 0000
> >     	1: Record 0001
> >     	2: Record 0002
> >     	3: Record 0003
> >     	4: Record 0004
> >     	Cursor done, rc=0

> >     	Last 5 records:
> >     	9: Record 0009
> >     	8: Record 0008
> >     	7: Record 0007
> >     	6: Record 0006
> >     	5: Record 0005