2012-03-29 100 views
2

在內容提供者上工作,我遇到問題。當我嘗試通過內容提供者更新SQLite數據庫中的某一行時,它會更新所有行中的列,而不僅僅是我指定的行。我知道CP正在工作,因爲我可以訪問它,使用它填充列表視圖,並更改列的內容,但不會只有一列。內容提供者更新所有行

下面是相關的更新方法

public int update(Uri url, ContentValues values, String where, 
      String[] whereArgs) { 
     SQLiteDatabase mDB = dbHelper.getWritableDatabase(); 
     int count; 
     String segment = ""; 
     switch (URL_MATCHER.match(url)) { 
     case ITEM: 
      count = mDB.update(TABLE_NAME, values, where, whereArgs); 
      break; 
     case ITEM__ID: 
      segment = url.getPathSegments().get(1); 
      count = mDB.update(TABLE_NAME, values, 
        "_id=" 
          + segment 
          + (!TextUtils.isEmpty(where) ? " AND (" + where 
            + ')' : ""), whereArgs); 
      break; 

     default: 
      throw new IllegalArgumentException("Unknown URL " + url); 
     } 
     getContext().getContentResolver().notifyChange(url, null); 
     return count; 
    } 

,這裏是我使用的代碼(嘗試)來更新它。

ContentValues mUpdateValues = new ContentValues(); 

mUpdateValues.put(ContentProvider.HAS, "true"); 
mUpdateValues.put(ContentProvider.WANT, "false"); 

mRowsUpdated = getContentResolver().update(Uri.parse(ContentProvider._ID_FIELD_CONTENT_URI 
+ rowId), mUpdateValues, null, null); 

這裏是URI

URL_MATCHER.addURI(AUTHORITY, TABLE_NAME + "/#", ITEM__ID); 

感謝,任何幫助,將不勝感激。

編輯我也試着

mRowsUpdated = getContentResolver().update(
        ContentProvider._ID_FIELD_CONTENT_URI, mUpdateValues, 
        null, null); 

mRowsUpdated = getContentResolver().update(
        ContentProvider.CONTENT_URI, mUpdateValues, 
        null, null); 

回答

2

你沒有指定WHERE子句,這是用來更新只特定行。內容提供者的默認行爲是更新所有行,除非您指定條件。

從文檔: developer.android.com/reference/android/content/ContentResolver.html

Parameters 
uri  The URI to modify. 
values The new field values. The key is the column name for the field. A null value will remove an existing field value. 
where A filter to apply to rows before updating, formatted as an SQL WHERE clause (excluding the WHERE itself). 
+0

我使用'rowId'嘗試這是一種基於記錄的_id爲字符串更新。我試過把它解析成'long',就像在WHERE _id ='rowId'' – 2012-03-29 21:18:10

+0

好吧,讓它工作,既然你是唯一的答案,並且基本上是正確的,因爲我的'WHERE'聲明搞砸了,我正在給你打上正確的名字。謝謝。 – 2012-03-29 22:22:41

+1

@BillGary爲了保持完整性,請使用您的最終解決方案更新您的問題。謝謝。 – 2013-01-25 20:34:18