将SQLite列更新为null而不影响其他列

时间:2014-11-05 05:38:23

标签: android sqlite

我的Android应用程序中有以下表格。我想删除(设置为null)所有数量的行。  enter image description here

我正在使用update语句来执行此操作。但它会产生错误

这是使用

的更新语句
 getContentResolver().update(MenuEntry.CONTENT_URI, null,
             MenuEntry.COLUMN_ITEM_QTY+ "='" + null + "'", null);

我的提供者类更新方法

 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;
}

这是错误日志

  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

有没有办法通过使用更新或删除语句来完成此操作?

1 个答案:

答案 0 :(得分:4)

像往常一样使用contentvalues进行更新。为特定键传递null

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