Cursing the cursor

It would seem that every step to upgrade to OS 6 and use SQLite for my BlackBerry java application comes with its own little headaches. I’m sure there is some sort of documentation somewhere that would make it a little easier to do this coding. But, until then, I’ll just muddle along.

The key reason for using SQLite is so that I can populate the device-side information easily.  Unfortunately, the data sent to the device is not in the best format at the moment.  So, I have to take sudo HTML tables and parse them out into data to be stored in the SQLite table.  But, before I can do that, I needed to figure out which table is being sent.  When the application initializes the database, it adds values to a configuration table of the table name and the table id.  When the data is loaded, I grab the table name and then look in the configuration table to determine the id.  Then with the id, I can use a switch statement to process the data using the parser associated for that table.  Simple enough.  But, when the code was retrieving the table name, it would error and crash trying to determine the id.  It took a bunch of break points and stepping to finally figure out the issue.

Here is the code to get an Value while passing in an Attribute

       Statement statement = myDataBase.createStatement("SELECT value FROM myTable WHERE attribute = ?");
            statement.prepare();
            statement.bind(1, attribute);
            Cursor cursor = statement.getCursor();
            Row row;
            //Use first cursor spot
            cursor.first();
            // Get the row from the cursor
            row = cursor.getRow();
            thisValue = row.getString(0);

            statement.close();
            cursor.close();

The issue I had was on the getString(0) part. Instead of the first position being at 1, the first position is at 0. Sort of obvious, but I guess I was looking at the bind statement, which has the first position at 1.

Another simple thing I determined today, not being the expert Java developer, is that I can call the static instance of the database connection without having to pass it through to a different class function.  In other words, I created the appDB connection when the application opens up and I initialize the database connection.  I do this on a class called SQLManager.  Therefor, when I want to use the database connection, I can just use SQLManager.appDB anywhere in application.  I wasn’t sure this would work, but after a little test, I found that it did work and will help cut down on code when calling functions to update tables.

About DeanLogic
Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.

About DeanLogic

Dean has been playing around with programming ever since his family got an IBM PC back in the early 80's. Things have changed since BASICA and Dean has dabbled in HTML, JavaScript, Action Script, Flex, Flash, PHP, C#, C++, J2ME and SQL. On this site Dean likes to share his adventures in coding. And since programming isn't enough of a time killer, Dean has also picked up the hobby of short film creation.

Leave a Reply

Your email address will not be published. Required fields are marked *

*