Getting null value from Sqlite database in android

Guys, I saved a null value in a sqlite database column

the value that is stored is return "null"

Now using the following code to get 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; 

Debugger shows str = "null"

but this condition does not work with str == null , str == "" , str == "null"

anyone can give me a hit on what to do.

Also resultset.isNull gives me false.

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

+4
source share
3 answers

If you saved a "null" literal string in the database, you compare it like this:

 if ( "null".equals(str) ) 

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

+7
source

Also resultet.isNull gives me false.

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

Debugger shows str = "null"

but this condition does not work 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 a null pointer exception error

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

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 crossover, make sure your field in db allows NULL (i.e. Not Null = 0)

hope this helps :)

0
source

Check the string String length.ike,

if (str.length == 0) {}

-one
source

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


All Articles