user1400538 user1400538 - 1 year ago 112
Android Question

Sqlite query returning 0 even if the value is null

Following is the query I use to fetch an int value corresponding to a particular field in my sqlite db table.

"SELECT conn_status FROM profiles WHERE devID = '" + id+"'"

If there is no value saved for 'conn_status' corresponding to the devID provided, then the existing value in the table would be null. I retrieve the data from java as shown:


The problem here is, with the given query , c.getInt returns 0 even if the value existing in the field is null. How can I modify this query such that, it returns a different value instead of 0, say 5, if the value is null.

Any help is appreciated.

Answer Source

You can use the isNull() function. Here's an example:

static int getInt(String columnName)
        return -1;
    return c.getInt(c.getColumnIndex(columnName));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download