Contents Index Lesson 2: Insert data into the database Lesson 4: Add synchronization to your application

UltraLite C++ User's Guide
  Tutorial: An Introductory Application

Lesson 3: Select the rows from the table


This lesson retrieves rows from the table, and prints them on the command line. It shows how to loop over the rows of a table.

To list the rows in the table

  1. Add the method below to customer.cpp. This method carries out the following tasks:

    bool do_select( Connection * conn ) {
      Table * table = conn->OpenTable( UL_TEXT("customer") );
      if( table == NULL ) {
         return false;
      }
      TableSchema * schema = table->GetSchema();
      if( schema == NULL ) {
       table->Release();
         return false;
      }
      ul_column_num id_cid    = schema->GetColumnID( UL_TEXT("id") );
      ul_column_num fname_cid = schema->GetColumnID( UL_TEXT("fname") );
      ul_column_num lname_cid = schema->GetColumnID( UL_TEXT("lname") );
      schema->Release();
      while( table->Next() ){
       ul_char fname[ MAX_NAME_LEN ];
       ul_char lname[ MAX_NAME_LEN ];
         table->Get( fname_cid ).GetString( fname, MAX_NAME_LEN );
         table->Get( lname_cid ).GetString( lname, MAX_NAME_LEN );
         _tprintf( "id=%d, name=%s %s\n", (int)table->Get( id_cid ), fname, lname );
      }
      table->Release();
      return true;
    }
  2. Add the following line to the main() method, immediately after the call to the insert method:

    do_select(conn);
  3. Compile and run your application, as in Lesson 1: Connect to the database.

    The application prints the following message:

    Connected to an existing database.
    The table has 2 rows.
    id= 1, name= Gene Poole
    id= 2, name= Penny Stamp

Contents Index Lesson 2: Insert data into the database Lesson 4: Add synchronization to your application