Code sample: Adding a schema to an SQLite database

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

class CreateDatabaseSchemaScreen extends MainScreen
{
    Database d;
    public CreateDatabaseSchemaScreen()
    {
        LabelField title = new LabelField("SQLite Create " + 
                                          "Database Schema Sample",
                                           LabelField.ELLIPSIS | 
                                           LabelField.USE_ALL_WIDTH);
        setTitle(title);
        add(new RichTextField("Adding a table to a database called " +
                              "MyTestDatabase.db on the SDCard."));
        try
        {
            URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                                   "MyTestDatabase.db"); 
            d = DatabaseFactory.openOrCreate(myURI);
            Statement st = d.createStatement( "CREATE TABLE People ( " +
                                              "Name TEXT, " +
                                              "Age INTEGER )" );
            
            st.prepare();
            st.execute();
            st.close();
            d.close();
        }
        catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }
             
    }
}

Was this information helpful? Send us your comments.