Code sample: Creating an SQLite database

By default, database files are stored on a media card. If you are using a BlackBerry Smartphone Simulator, you might need to Simulate a media card.

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.*;
import net.rim.device.api.ui.*;

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

class CreateDatabaseScreen extends MainScreen
{
    Database d;
    public CreateDatabaseScreen()
    {
       LabelField title = new LabelField("SQLite Create Database Sample", 
                                          LabelField.ELLIPSIS | 
                                          LabelField.USE_ALL_WIDTH);
       setTitle(title);
       add(new RichTextField("Creating a database called " +
                             "MyTestDatabase.db on the SDCard."));
       try
       {
           URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" + 
                                  "MyTestDatabase.db"); 
           d = DatabaseFactory.create(myURI);
           d.close();
       }
       catch ( Exception e ) 
       {         
           System.out.println( e.getMessage() );
           e.printStackTrace();
       }
    }
}
Previous topic: Create an SQLite database

Was this information helpful? Send us your comments.