views:

329

answers:

1

After many houres of bug searching in a big app, I have finally tracked down the bug. This logging captures the problem:

Log.d(TAG,"buildList, DBresult.getInt(1): "+DBresult.getInt(1));
Log.d(TAG,"buildList, DBresult.getString(1): "+DBresult.getString(1));
Log.d(TAG,"buildList, DBresult.getInt(4): "+DBresult.getInt(4));
Log.d(TAG,"buildList, DBresult.getString(4): "+DBresult.getString(4));

The resulting output:

05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getInt(1): 0
05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getString(1): false
05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getInt(4): 0
05-06 11:11:20.123: DEBUG/TodoList(18943): buildList, DBresult.getString(4): true

There are no backgroung threads running. As you can see the problem is that '0' is interpreted as false on one occasion, and as true on another. Since I am completely lost on how this can happen, I dont know how to proceed. What could possibly result in such a behaviour? Both the columns are of the type "boolean", i.e a numeric in sqlite. Unfortunately the string returned is the correct value, while the integer is always 0. If I export the database to my computer and check it with SQlite Administrator I can see that the values are correctly set, it is only the getInt()-function that always returns 0. I know for a fact that this works in other apps I have coded, and I dont know why this has stopped working.

I have tried compiling the code under 2.0, 2.0.1 and 2.1, and it always appears. I can make my app runnable again by getting boolean values like this:

myBool= (DBresult.getString(0).equals("true"));

but that is both ugly and not optimized. Any suggestions on what is causing this behaviour is welcome.

Cheers,

A: 

I do believe I solved it: After an update to the database interface all data before insertion was converted to string via String.valueOf()-function. So instead of "1", "true" was input into the database. The difference was not shown with my databas management-tools. Why this is coverted to '0' with the getInt()-function I do not know, but this seems to be the problem.

sandis
So you're saying the column type was not 'boolean', but accidentally converted to a 'string' type column?
Kieveli
Not exactly. The column was defined with something like "myColumn boolean not null". But boolean does not really exist in sqlite, but are supposed to be saved as an int, as 0 or 1 (http://www.sqlite.org/datatype3.html). So while I used to just place the boolean in an ContentValues, after the update i instead placed String.valueOf(boolean) in the ContentValues, i.e the String "true" or "false". To be honest I dont know enough about databases to understand how this was interpreted, but it resulted in my weird errors, and unfortunately the databas management-tools did not explain the problem.
sandis
The getInt() will return you the value of the field that you have specified. But if that field is not of type Integer then it will use Integer.valueOf(), which is returning 0 as its not able to convert string "true".
Karan
that makes some sense, even though I would almost have preferred an exception to this erratic behaviour. Thanks though, for helping me understand what happened :)
sandis