Search code examples
sqliteblackberryblackberry-simulator

Loss of data when switching classes in Blackberry


I have incorporated a database into my Blackberry application. When I start the application the database builds, and it inserts values into the database. However when I change the class i.e using this

        HelloBlackBerryScreen bbScreen = new HelloBlackBerryScreen();
        UiApplication.getUiApplication().pushScreen(bbScreen);
        this.invalidate();

when I reload the previous class I get this:

 [0.0] FileIO:fileinfo start 5e2
 [0.0] FileIO:info by name complete 5e2
 [0.0] FileIO:fileinfo start 5e3
 [0.0] FileIO:info by name complete 5e3
 [0.0] FileIO:open start 5e4
 [0.0] FileIO:open complete 5e4
 [0.0] FileSystem:bad open status 12
 [0.0] File system error (12)read error here
 [0.0] No stack trace

When the application initally starts, it reads the database fine, and certain values are loaded in the the relevant areas, then I switch to another class, which also can access the database, and then switch back to the first class and I get that error. Am I not closing the database properly when I go into the next class?

Here is the code I use to get the database values:

public void getValues(){
        try {
            URI uri = URI.create("file:///SDCard/Databases/" + "database2.db");
            sqliteDB = DatabaseFactory.open(uri);
            Statement st = sqliteDB.createStatement("SELECT Signauto,SaveP FROM options");
            st.prepare();
            Cursor c = st.getCursor();
            Row r;
            int i = 0;
            while(c.next()) 
            {
                r = c.getRow();
                i++;
                System.out.println(r.getString(0) + "HERE");
                System.out.println(r.getString(1) + "HERE");
                if(r.getString(0).equals("true")){
                    tickBoxes[1] = true;
                    //signAuto();

                }
                else{
                    tickBoxes[1] = false;
                }
                if(r.getString(1).equals("true")){
                    tickBoxes[0] = true;
                    setUserPass();
                }
                else{
                    tickBoxes[0] = false;
                }

            }

            if (i==0)
            {
                System.out.println("No data in the options table.");
            }
            st.close();
            sqliteDB.close();
        }

        catch ( Exception e ) {
            System.out.println( e.getMessage() + "read error here");
            e.printStackTrace();
        }
    }

Solution

  • A few things to try.

    Move the close statements into their own catch blocks so that an exception in one doesn't prevent the other from closing.

        } finally {
            try {
                if (st!=null) st.close();
            } catch (DatabaseException e) {
                System.out.println( e.getMessage() + "read error here");
            }
            try {
                if (sqliteDB!=null) sqliteDB.close();
            } catch (DatabaseIOException e) {
                System.out.println( e.getMessage() + "read error here");
            }
        }
    

    Also synchronize your methods accessing the DB so that one doesn't cause an exception in another.