SQLite can insert but not select - work in Simulator

BlackBerry Java plug-in for Eclipse Version: 1.5.0
Built with OS 6

Device with OS 7.1 9930

The title basically sums it up.

I am able to create a database, create the table and insert values into the table.  In the Simulator, the select statement to display the data works.  But when I try to debug on the device, the select statement returns with an empty cursor.

I installed the Manager of SQLite in FireFox, so I am able to stop debugging and veryify that the database on the SD card in the device exists and that he has made.  In fact, apparently, whenever I tried to debug, it would be just to reinsert the records.

Because I thought it might be an opening and closing of the issue of the database, I give the floor to open the database for the insert and select.  I'm sure it's something simple escapes me, but I can't understand why it works on the Simulator, but not on the device.

The Insert statement

public static void insert(String name, String value, Database db) throws DatabaseException
    {
        try
        {
            // Open Database
            db = DatabaseFactory.open(appAttributes.dbName);
            // INSERT a row into the Category table for the new category
            Statement statement = db.createStatement("INSERT INTO " + tableName + "(name, value) VALUES(?, ?)");
            statement.prepare();
            statement.bind(1, name);
            statement.bind(2, value);
            statement.execute();
            statement.close(); 

            db.close();
        }
        catch(DatabaseException dbe)
        {
            SDApp.errorDialog(dbe.toString());
        }
    }

The select statement

public static String getValueByName(String name, Database db)
    {
        String thisValue = null;

        try
        {
            //Open Database
            db = DatabaseFactory.open(appAttributes.dbName);
            // Read in all records from the Category table
            Statement statement = db.createStatement("SELECT name, value FROM " + tableName + " WHERE name = ?");
            statement.prepare();
            statement.bind(1, name);
            Cursor cursor = statement.getCursor();
            Row row;

            if(cursor.isEmpty()){
                thisValue = "Cursor Is Empty";
            } else {
                cursor.first();
                row = cursor.getRow();
                thisValue = row.getString(1);
            }

            statement.close();
            cursor.close();
            // Close Database
            db.close();
        }
        catch(DatabaseException dbe)
        {
            SDApp.errorDialog(dbe.toString());
        }
        catch(DataTypeException dte)
        {
            SDApp.errorDialog(dte.toString());
        }

        return thisValue;
    }

Okay, I found the solution.

Apparently, the example should not be taken exactly.  This blog post on the use of SQLite , I found that you need to open the database, but it will not close until you close the application.  And then just call the same database open initially for all other calls reporting.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for