Search code examples
androidsqlsqliteandroid-sqlite

Android SQLite, How to know if a sqlite field is NULL or 0?


for example, there is a student table:

CREATE TABLE students (name TEXT, grade INTEGER);

and I insert an entry:

INSERT INTO students(name) VALUES('Allen');

And you can see the grade is NULL. In Android, I use these code to read the table:

Cursor cursor = database.rawQuery("SELECT grade FROM students WHERE name = 'Allen'", null);
cursor.moveToFirst();
long serverId = cursor.getLong(cursor.getColumnIndexOrThrow("grade"));
//the serverId is 0
cursor.close();

I found the cursor.getLong returns 0, but the real values in SQLite is NULL. So how can I distinguish between the 0 and the NULL?


Solution

  • Use cursor.isNull(cursor.getColumnIndexOrThrow("grade")) to tell if the column is null