san88
san88

Reputation: 1384

Update SQLite Columns to null without affecting to other columns

I have following table in my android app. I want to delete (set to null) all qty rows. enter image description here

Im using update statement to do it.but it genarate errors

here is the update statement im using

 getContentResolver().update(MenuEntry.CONTENT_URI, null,
             MenuEntry.COLUMN_ITEM_QTY+ "='" + null + "'", null);

my provider class update method

 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

     final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
     final int match = sUriMatcher.match(uri);
     int rowsUpdated;


     switch (match) {
         case MENU:
             rowsUpdated = db.update(OrderContract.MenuEntry.TABLE_NAME, values, selection,
                     selectionArgs);
        if (rowsUpdated != 0) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
             break;
         case MENU_ID:
             rowsUpdated = db.update(OrderContract.MenuEntry.TABLE_NAME, values, selection,
                     selectionArgs);
             break;   
         case ORDER:
             rowsUpdated = db.update(OrderContract.OrderEntry.TABLE_NAME, values, selection,
                     selectionArgs);

           if (rowsUpdated != 0) {
           getContext().getContentResolver().notifyChange(uri, null);
       }
             break;
         case ORDER_ID:
             rowsUpdated = db.update(OrderContract.OrderEntry.TABLE_NAME, values, selection,
                     selectionArgs);
             break;     
         default:
             throw new UnsupportedOperationException("Unknown uri: " + uri);
     }

     return rowsUpdated;
}

here is the error log

  11-05 11:01:14.241: E/AndroidRuntime(7212): java.lang.RuntimeException: Unable to start service nk.co.toks.android.kdisplay.StatusUpdateService@413c4c30 with Intent 
    { cmp=com.resbook.ressdapp/nk.co.toks.android.kdisplay.StatusUpdateService (has extras) }:
     java.lang.IllegalArgumentException: Empty values
    11-05 11:01:14.241: E/AndroidRuntime(7212): Caused by: java.lang.IllegalArgumentException: Empty values
    11-05 11:01:14.241: E/AndroidRuntime(7212):     at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1531)
    11-05 11:01:14.241: E/AndroidRuntime(7212):     at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)
    11-05 11:01:14.241: E/AndroidRuntime(7212):     at android.content.ContentProvider$Transport.update(ContentProvider.java:235)
    11-05 11:01:14.241: E/AndroidRuntime(7212):     at android.content.ContentResolver.update(ContentResolver.java:1020)

    11-05 11:01:14.241: E/AndroidRuntime(7212):     at android.app.Service.onStartCommand(Service.java:450)
    11-05 11:01:14.241: E/AndroidRuntime(7212):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)
    11-05 11:01:14.241: E/AndroidRuntime(7212):     ... 10 more

Is there any way to complete this by using update or delete statement?

Upvotes: 2

Views: 1158

Answers (1)

Aun
Aun

Reputation: 1923

update as usual with contentvalues. pass null for the specific key

    ContentValues values = new ContentValues();
    values.putNull(MenuEntry.COLUMN_ITEM_QTY);
    int updatedRows = getContentResolver().update(MenuEntry.CONTENT_URI, values, null, null);

Upvotes: 4

Related Questions