Search code examples
blackberryblackberry-simulatorsqlite

How to use sqlite database in Blackberry simulator?


Hi I am new to blackberry development.

I am trying to create a small demo app of sqlite. for that i am writing the following code:

    try
    {
        URI myURI = URI.create("file:///SDCard/Databases/myDb.db"); 
        d = DatabaseFactory.openOrCreate(myURI);
        d.close();
        add(new RichTextField("DB created successfully"));
    }
    catch ( Exception e ) 
    {         
        System.out.println( e.getMessage() );
        e.printStackTrace();
        add(new RichTextField("Error: "+e.toString()));
    }

when i run this i am getting a exception like this

net.rim.device.api.database.DatabasePathException: Invalid path name. Path does not contains a proper root list. See FileSystemRegistry class for details.

i have been set Sdcard in Simulator: Simulate-->change sdcard-->Add directories (E:\mediacard)


Solution

  • After setting up the SDCard, locate the SDCard from the file browser of Simulator to check if it is installed correctly.

    To get idea about the best practices when working with SQLite database, please check the SQLite sample application provided in http://docs.blackberry.com. If you are using BlackBerry Plugins For Eclipse then you can import SQLiteDemo application and check the code. Following lines of codes is from the constructor of SQLiteDemo class.

    // Determine if an SDCard is present 
    boolean sdCardPresent = false;
    String root = null;
    Enumeration e = FileSystemRegistry.listRoots();
    while (e.hasMoreElements()) {
        root = (String)e.nextElement();
        if(root.equalsIgnoreCase("sdcard/")) {
            sdCardPresent = true;
        }     
    }            
    if(!sdCardPresent) {
        // no database can't be created
    }          
    else {
        // create database
    }