Getting null ints from sqlite in android

I have a problem, which may be a design problem, but I'm struggling to find a way to overcome it.

In my sqlite database in my Android application, I have a table called Customer . This has about 40 columns of various string and int data types. In practice, any of these columns may or may not be null.

In my code, I have a function, simply called getCustomer() , which queries the database for a particular customer and puts all of their cells from the database into the Customer class, which contains the variables for each column. Then I can transfer this client object as I wish. The getCustomer() function returns this Customer object.

My problem is integers, which can be zero. I am familiar with how int cannot be null in java, but how Integer can be null. But my problem is that the cells in the database may be empty (e.g. null).

For string columns, I just do this:

 Customer cust = new Customer(); cust.firstName = cursor.getString(0); 

If cursor.getString(0); returns a null value, then the variable firstName to null. Nice and simple. However, using int:

 Customer cust = new Customer(); cust.daysSinceJoining = cursor.getInt(5); 

The above runtime crash if daysSinceJoining is NULL. So I tried the following:

 Customer cust = new Customer(); if (cursor.getInt(5) != null) cust.daysSinceJoining = cursor.getInt(5); 

However, this gives me a compiler error, since you cannot use int in null comparison.

How can I get around this problem? How can I get int from sqlite database when int value can be null?

+4
source share
5 answers

You can try the isNull() function.

+9
source

@sanders is right about the isNull() method, and here is how you edit your code to use it:

 Customer cust = new Customer(); if (!cursor.isNull(5)) cust.daysSinceJoining = cursor.getInt(5); 
+10
source

Please take a look at this answer:

Invalid getInt restrictions

I think this will work for you:

 int lIndex = cursor.getColumnIndexOrThrow(COLUMN); Integer lInteger = null; if (!cursor.isNull(lIndex) lInteger = cursor.getInt(lIndex); 
+5
source

You can create a cursor wrapper and add new functionality to the cursor class:

 public class CustomCursor extends CursorWrapper { public CustomCursor(Cursor cursor) { super(cursor); } //simple constructor public Integer getInteger(int columnIndex) { // new method to return Integer instead of int if (super.isNull(columnIndex)){ return null; }else{ return super.getInt(columnIndex); } } } 

Usage example:

 Cursor defaultCursor = db.rawQuery("select null,2 ", null); defaultCursor.moveToFirst(); CustomCursor customCursor = new CustomCursor(defaultCursor); customCursor.moveToFirst(); //custom cursor can do anything that default cursor can do int defaultInt0 = defaultCursor.getInt(0); //nulls are usually converted into zero int defaultInt1 = defaultCursor.getInt(1); //2 is correct int customInt0 = customCursor.getInt(0); //these 2 are same as above , ie zero and 2 int customInt1 = customCursor.getInt(1); Integer customInteger0 = customCursor.getInteger(0); // this will give a null Integer Integer customInteger1 = customCursor.getInteger(1); // this will give a 2 Integer Log.v("custom log.v call ", "lets see what outputs, null is usually converted to the word 'null' by the String.valueOf method :" +String.valueOf(defaultInt0)+","+String.valueOf(defaultInt1)+"," +String.valueOf(customInt0)+","+String.valueOf(customInt1)+"," +String.valueOf(customInteger0)+","+String.valueOf(customInteger1) ); //V: lets see what outputs, null is usually converted to the word 'null' by the String.valueOf method :0,2,0,2,null,2 
0
source

if (cursor.getInt (5)! = 0) {

 cust.daysSinceJoining = cursor.getInt(5); 

}

or

 int index = cursor.getColumnIndex(KEY_NAME); Integer x = null; if (!cursor.isNull(index) cust.daysSinceJoining = cursor.getInt(5); } 

see getInt () documentation :

-2
source

Source: https://habr.com/ru/post/1495284/


All Articles