4

Guys i have stored a null value in the column of sqlite database

the value that is storing is return "null"

Now using following code to retrieve it

int i = resultSet.getInt(resultSet.getColumnIndex(columnName));
String str = resultSet.getString(resultSet.getColumnIndex(columnName));
boolean bool = resultSet.isNull(resultSet.getColumnIndex(columnName));

if( str == null ) {
    return -1;
}            
return i;

The debugger shows the value of str = "null"

but this condition is not working with str == null , str == "" , str == "null"

anyone can give me a hit of what to do..

Also resultset.isNull gives me a value of false.

So how can i store null in database, secondly if i use "null" then how can i make this condition work.

1
  • Plz add the code where you are adding null value to database. Commented Feb 7, 2012 at 5:20

3 Answers 3

7

If you have stored the "null" literal string in the database, you compare against it like so --

if ( "null".equals(str) )

To store a null value in your database table, you use PreparedStatement.setNull()

Sign up to request clarification or add additional context in comments.

3 Comments

can u give an idea of how can i use these preparedStatement? any small example
PreparedStatment might be overkill. You can probably just use sqlitedb.execSQL("insert into yourtable values(null, ...");
mDb.execSQL(updateQuery, valVars); USING this and null as one of the arguments stored in valVar but it gives error of null pointer exception
0

Also resultset.isNull gives me a value of false.

resultset.isNull will give true if your your value = null. But as you said your value = "null" , which is a String.

The debugger shows the value of str = "null"

but this condition is not working with str == null , str == "" , str == "null"

try comparing with str.equals("null")

mDb.execSQL(updateQuery, valVars); USING this and null as one of the arguments stored in valVar but it gives error of null pointer exception

As sepcified on http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html this method should be used to - Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.

Please try using update(String table, ContentValues values, String whereClause, String[] whereArgs) or updateWithOnConflict(String table, ContentValues values, String whereClause, String[] whereArgs, int conflictAlgorithm). Also please cross verify that your field in db allows NULL(i.e. Not Null = 0)

hope this helps :)

1 Comment

actually i need to run sql statements and using Update and others i can't use sql statements...
-1

Check for String length.like,

if(str.length == 0){ }

2 Comments

yes you right .But if nothing in string means length is zero right?
No one discussed "nothing in string".

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.