Code sample: Retrieving table data

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.database.*;
import net.rim.device.api.io.*;

public class ReadData extends UiApplication
{
    public static void main(String[] args)
    {
        ReadData theApp = new ReadData();
        theApp.enterEventDispatcher();
    }
    
    public ReadData()
    {
        pushScreen(new ReadDataScreen());
    }   
}

class ReadDataScreen extends MainScreen
{
    Database d;
    public ReadDataScreen()
    {
        LabelField title = new LabelField("SQLite Read Table Data Sample",
                                           LabelField.ELLIPSIS | 
                                           LabelField.USE_ALL_WIDTH);
        setTitle(title);
        add(new RichTextField("Attempting to retrieve data from " + 
                              "MyTestDatabase.db on the SDCard."));
        try
        {
            URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                                   "MyTestDatabase.db"); 
            d = DatabaseFactory.open(myURI);
            
            Statement st = d.createStatement("SELECT Name,Age FROM People");

            st.prepare();
            Cursor c = st.getCursor();
            
            Row r;
            int i = 0;
            while(c.next()) 
            {
                r = c.getRow();
                i++;
                add(new RichTextField(i + ". Name = " + r.getString(0) +
                                          " , " +
                                          "Age = " + r.getInteger(1)));
            }
            if (i==0)
            {
                add(new RichTextField("No data in the People table."));
            }
            st.close();
            d.close();

        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }          
    }
}

Was this information helpful? Send us your comments.