0

I try to get an Item out of my SQL db in android, everything goes well, the only thing I am struggling with is to get the INTEGER value is there is nothing filled in in the DB (null?)

So I first initialize it, then get the Integer value out of the DB and then assign it to an instance of my class. I was once told that if it will get nothing out of the DB it will keep the initialized value, but this might be wrong? I use now:

int score = 100;
score = c.getInt(11);
q.setScore(score);
6
  • and if c is your cursor than see how you wrote query to select from table.Is there any data in the table.Data in 11 th column is integer type? Commented Oct 8, 2012 at 12:11
  • C is indeed the cursor, it is collecting data for sure, I can assure you that the field is empty (if I export the database from device I see an empty SQL field) and the type is Numeric ... by typing this I realize that this might be the problem? Or is Numeric ok? q is my instance of my class. if (c.moveToFirst()) { Question q = new Question(); Commented Oct 8, 2012 at 12:14
  • If the field is empty then what number do you expect getInt to return? I guess it will return null so you will loose the old value(100) Commented Oct 8, 2012 at 12:17
  • 100 as pointed int my code above.. if there is actually a value in the DB it should set the score var of that instance to the value of the db.. Commented Oct 8, 2012 at 12:18
  • Is it possible to post the query you used to create the db? Commented Oct 8, 2012 at 12:23

2 Answers 2

1

I was once told that if it will get nothing out of the DB it will keep the initialized value

That is true for Embedded SQL. You seem to be using a resultset object, so it is not true. Resultset objects typically return 0 if you try to get a numeric null value from a column.

Use c.wasNull() to check if the value you just read was null and not 0.

EDIT (added the code from Jack's comment):

int score; 
if (c.isNull(11)) { 
  score = 100; 
} else { 
  score = c.getInt(11); 
} 
q.setScore(score);
Sign up to request clarification or add additional context in comments.

2 Comments

I am using this configuration for the sql db: reigndesign.com/blog/… ... is this embedded?
int score; if (c.isNull(11)){ score = 100; } else { score = c.getInt(11); } q.setScore(score); This solved it for me! Thanks!
0

http://developer.android.com/reference/android/database/Cursor.html#getInt(int)

*public abstract int getInt (int columnIndex) Since: API Level 1 Returns the value of the requested column as an int. The result and whether this method throws an exception when the column value is null, the column type is not an integral type, or the integer value is outside the range [Integer.MIN_VALUE, Integer.MAX_VALUE] is implementation-defined. Parameters columnIndex the zero-based index of the target column. Returns the value of that column as an int.*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.