Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.0 » UltraLite - C and C++ Programming » Tutorials » Tutorial: Build an application using the C++ API

 

Lesson 3: Select and list rows from the table

The following procedure retrieves rows from the table and prints them on the command line.

Listing rows in the table

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

    • Opens the Table object.
    • Retrieves the column identifiers.
    • Sets the current position before the first row of the table.

      Any operations on a table are carried out at the current position. The position may be before the first row, on one of the rows of the table, or after the last row. By default, as in this case, the rows are ordered by their primary key value (cust_id). To order rows in a different way, you can add an index to an UltraLite database and open a table using that index.

    • For each row, the cust_id and cust_name values are written out. The loop carries on until the Next method returns false, which occurs after the final row.
    • Closes the Table object.
    bool do_select( Connection * conn ) {
      Table * table = conn->OpenTable( _TEXT("ULCustomer") );
      if( table == UL_NULL ) {
        return false;
      }
      TableSchema * schema = table->GetSchema();
      if( schema == UL_NULL ) {
        table->Release();
        return false;
      }
      ul_column_num id_cid = 
         schema->GetColumnID( UL_TEXT("cust_id") );
      ul_column_num cname_cid = 
         schema->GetColumnID( UL_TEXT("cust_name") );
      
      schema->Release();
    
      _tprintf( _TEXT("\n\nTable 'ULCustomer' row contents:\n") );
    
      while( table->Next() ) {
        ul_char cname[ MAX_NAME_LEN ];
        
        table->Get( cname_cid ).GetString(
                    cname, MAX_NAME_LEN );
        
        _tprintf( _TEXT("id=%d, name=%s \n"), (int)table->Get(id_cid), cname);
      }
      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 your application by running nmake.

  4. Run your application by typing customer at the command prompt.