Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Cyriel
Posts: 19
Registered: ‎08-26-2008
My Device: Not Specified
Accepted Solution

database does not get created

[ Edited ]

I'm trying to create a database as outlined in the example documentation. However whateve rI do the databse does not get created. All seems to work fine. There are no error messages. But whatever I create as URI the databse doe not get created. I can even fill in complete bogus in th estring and still there will be no error. Anyone can see where this goes wrong? I'm doing this in simulators, not on real devices. Code below:

 

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

 

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: database does not get created

Have you tried it with the call to close() not commented out? Also, use DatabaseFactory.openOrCreate() instead of DatabaseFactory.create() if this code is going to be used more than once.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Contributor
Cyriel
Posts: 19
Registered: ‎08-26-2008
My Device: Not Specified

Re: database does not get created

it was fixed by adding an SD card by defualt in Eclipse (memory section). Appaerently it does not function when I just add the SD card after the boot in the simulator.

Contributor
vituvian
Posts: 17
Registered: ‎12-01-2010
My Device: Not Specified

Re: database does not get created

after much trouble i created the database file  in the  sd card with no content at all... it simply wont create tables.... only after many sessions of refreshing and loading other applications and changing SD card tables will  be created ! why des that happen? Inserting Data is the same story .... i never obtained an output.... plz help me... What am i doing rong

 

 

package dbs;
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 CreateDatabase extends UiApplication {
    public CreateDatabase() {
        pushScreen(new InnerClassScreen());
    }
    public static void main(String[] args){
        CreateDatabase firstSQLiteApp = new CreateDatabase();
        firstSQLiteApp.enterEventDispatcher();
    }
    class InnerClassScreen extends MainScreen {
        public Database sqliteDB;
        public InnerClassScreen() {
            LabelField title = new LabelField("Create DB Application",
                LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
            setTitle(title);
            add(new RichTextField("Initializing db create process..."));
            try{
                URI uri = URI.create(
                    "/SDCard/database1.db");
                sqliteDB = DatabaseFactory.openOrCreate(uri);
                add(new RichTextField(
                    "Status: Database was successfully created."));
            } catch (Exception e){
                System.out.println(e.getMessage());
                add(new RichTextField(
                    "Status: Database was not created."));
                add(new RichTextField(e.getMessage()));
                e.printStackTrace();
            }
        
try {
    URI myURI = URI.create("/SDCard/database1.db");
    sqliteDB = DatabaseFactory.openOrCreate(myURI);
    Statement st = sqliteDB.createStatement( "CREATE TABLE 'Employee' ( " +
                                              "'Name' TEXT, " +
                                              "'Age' INTEGER )" );
    st.prepare();
    st.execute();
}
catch ( Exception e ) {
    System.out.println( e.getMessage() );
    e.printStackTrace();
    
    try {
        URI myURI = URI.create("//file/SDCard/database1.db");
        sqliteDB = DatabaseFactory.openOrCreate(myURI);
        Statement st = sqliteDB.createStatement(
            "INSERT INTO Employee(Name,Age) " +
            "VALUES ('Ralph',47,'peter',23,'alex',67)");
        st.prepare();
        st.execute();
    }
    catch ( Exception e1 ) {
        System.out.println( e1.getMessage() );
        e1.printStackTrace();
    }
    try {
        URI myURI = URI.create("/SDCard/database1.db");
        sqliteDB = DatabaseFactory.openOrCreate(myURI);
        Statement st = sqliteDB.createStatement("SELECT * FROM Employee");
        st.prepare();
        Cursor c = st.getCursor(); 
        int i=0;
        Row r;
        while(c.next()) {
            r = c.getRow();
            i++;
            add(new RichTextField(i + ". Name = " + r.getString(0) +
                    " , " +
                    "Age = " + r.getInteger(1))); }
    }
    catch ( Exception e3 ) {
        System.out.println( e3.getMessage() );
        e3.printStackTrace();
    }
}}}}