Create an SQLite database

Before you begin:

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.

  1. Import the required libraries.
    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.*;
  2. Create the framework for the application by extending the UiApplication class. This class represents your application. Provide a main() method for the new class. In the main() method, create an instance of the new class and invoke the enterEventDispatcher() method to enable the application to receive events. Provide a constructor for the new class. In the constructor, invoke the pushScreen method to display the custom screen for the application.
    public class CreateDatabase extends UiApplication
    {
        public static void main(String[] args)
        {
            CreateDatabase theApp = new CreateDatabase();
            theApp.enterEventDispatcher();
        }
        
        public CreateDatabase()
        {
            pushScreen(new CreateDatabaseScreen());
        }   
    }
  3. Create the screen for the application by extending the MainScreen class. Provide a constructor for the new class. In the constructor, create the title for the screen with a LabelField object and display it by invoking the setTitle() method. Invoke the add() method to display a text field on the screen.
    class CreateDatabaseScreen extends MainScreen
    {
       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."));
       }
    }
  4. Create a URI that represents the database file by invoking the static create() method of the URI class. Invoke the create() method of the DatabaseFactory class to create the database that corresponds to the URI that you created. This creates an SQLite database on the microSD card of the BlackBerry device. If you do not specify a full path, the database is created in a folder named after your application.
    try
    {
       URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
       "MyTestDatabase.db");
       Database db = DatabaseFactory.create(myURI);
    }
    catch ( Exception e )
    {
       System.out.println( e.getMessage() );
       e.printStackTrace();
    }
  5. When you are finished using the database, it is good practice to close it.
    db.close();
After you finish:

After creating a database, verify that the database file was created. You can do this in any of the following ways:

  • View the database in a database viewer
  • Look in the file system for the database file, and verify that it is not zero size
  • Invoke DatabaseFactory.exists()
Previous topic: Character encoding

Was this information helpful? Send us your comments.