Accessing a field without loading a whole record: Tutorial

This tutorial shows you how to access a field without loading a whole record.

Before you start, you must understand:

  • the general concept of the Comms Database

  • the specific concept of fields, records, links and tables

  • how to write and build application code to run on Symbian platform

This tutorial shows you how to get the value of a field in the GlobalSettings table. Records from the GlobalSettings table are not loaded.

This tutorial uses the BearerAvailablityCheckTSY field. This field contains the name of the TSY used to check for bearer availability.

  1. Make sure that you have created a session.
  2. Create the field object in the tool or application process. You construct a CMDBField <T> object and pass KCDTIdBearerAvailabilityCheckTSY to the constructor. The symbol KCDTIdBearerAvailabilityCheckTSY defines the unique numeric Id for the BearerAvailablityCheckTSY field.
            
             
            
            ...
    
    // Create the field object. The template parameter is TDesc, because the field value
    // is a short text description. 
    // Use the "... new(Eleave).. " construction to create the field object.
    // Pass KCDTIdBearerAvailabilityCheckTSY as a parameter to the constructor.
    
    CMDBField<TDesC>* descField = new(ELeave) CMDBField<TDesC>(KCDTIdBearerAvailabilityCheckTSY);
    ...
           
  3. Set the data that identifies the field and load the field A descriptor field needs an additional call to set the size of the descriptor. Call SetMaxLengthL() to set the size of the descriptor. This function is a member of the field class CMDBField <TDesC> . Internally, the function causes the allocation of a descriptor. You must set the size of the descriptor before you assign the data. You can also use the SetL() function. This function sets the length and assigns the data. The function is a member of the field class CMDBField <TDesC> .
            
             
            
            ...
    
    // The symbol "KMaxTextLength" defines the maximum length of short text.
    // The record Id is ALWAYS 1 for global settings table.
    descField->SetMaxLengthL(KMaxTextLength);
    descField->SetRecordId(1); //recorded is ALWAYS 1 for global settings table
    
    // Load the field.
    descField->LoadL(*iDb);
    ...
           
  4. Check that the field has a value before you use it
            
             
            
            ...
    // Use the IsNull() function to check that the field has a value.
    // In this example, the code leaves if the field does not have a value.
    // Your code needs to handle a Null value appropriately.
    
    if(descField->IsNull())
        {
        User::Leave(KErrNotFound);
        }
    else
        {
        // Either process the content of *descField in place,
        // or copy the content to another descriptor. 
        // The processing depends on the structure of your code.
        // In this example, the content is copied into an RBuf for
        // further processing.
        RBuf buf;
        buf.CleanupClosePushL(); 
        buf.CreateL(descField);
        ...
        // Do something with the data.
        ...
        // Delete the RBuf buffer
        CleanupStack::PopAndDestroy()   
        ...
        }
    
    // Do not forget to delete the field object.
    delete descField;
    ...