Code sample: Listing database tables

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 ListTables extends UiApplication
{
    public static void main(String[] args)
    {
        ListTables theApp = new ListTables();
        theApp.enterEventDispatcher();
    }
    
    public ListTables()
    {
        pushScreen(new ListTablesScreen());
    }   
}

class ListTablesScreen extends MainScreen
{
    Database d;
    public ListTablesScreen()
    {
        LabelField title = new LabelField("SQLite List Database Tables", 
                                           LabelField.ELLIPSIS | 
                                           LabelField.USE_ALL_WIDTH);
        setTitle(title);
        add(new RichTextField("Attempting to list tables in " +
                              "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 FROM " +
                                             " sqlite_master " +
                                             "WHERE type='table'" +
                                             "ORDER BY name");
            
            st.prepare();
            Cursor c = st.getCursor();
            
            Row r;
            int i = 0;
            while(c.next()) 
            {
                r = c.getRow();
                i++;
                add(new RichTextField(i + ". Table: " + r.getString(0)));
            }
            if (i==0)
            {
                add(new RichTextField("There are no tables " +
                                      " in the MyTestDatabase database."));
            }
            st.close();
            d.close();
            
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
             
    }
}

Was this information helpful? Send us your comments.