Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android cursor how to get null values from columns

I keep all data to be stored in database as Objects (ex. instead of int I use Integer) in order to keep null values.

When saving data to SQLite database if Object is null I use:

statement.bindNull(index);

When I look at my database using aSQLiteManager it seems that values are correctly stored (empty field for Long column with null value).

However how to read value from cursor keeping null information?

If I use:

cursor.getLong(7)

for column where I have null, instead of null I get 0. It's not surprising as getLong returns long type value, not Long. However I'm not able to find any function returning Long. Any help? Of course I need also get functions keeping null values for other Objects (Integer, String etc.).

like image 955
user2707175 Avatar asked Oct 30 '13 23:10

user2707175


People also ask

What is SQLitedatabase in Android?

SQLite Database is an open-source database provided in Android which is used to store data inside the user's device in the form of a Text file. We can perform so many operations on this data such as adding new data, updating, reading, and deleting this data.

Which function is used for moving the cursor to the specified row in Android?

moveToNext(): Moves the cursor to the next row relative to the current position. boolean Cursor.

What is the use of cursor explain with example in Android?

A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory. To get the number of elements of the resulting query use the getCount() method.


1 Answers

You need to check if it's null yourself.

Long l = null;
if (!cursor.isNull(7))
    l = cursor.getLong(7);
like image 131
Graham Borland Avatar answered Sep 21 '22 11:09

Graham Borland