2013-07-31 29 views
0

ContentProvider中SQLite更新的奇怪行爲。更新後的SQLite結果相同

更新方法:

@Override 
public int update(Uri uri, ContentValues updateValues, String whereClause, String[] whereValues) { 

    SQLiteDatabase db = TasksContentProvider.dbHelper.getWritableDatabase(); 
    int updatedRowsCount; 
    String finalWhere; 

    db.beginTransaction(); 
    // Perform the update based on the incoming URI's pattern 
    try { 
     switch (uriMatcher.match(uri)) { 
     case MATCHER_TASKS: 

       updatedRowsCount = db.update(TasksTable.TABLE_NAME, updateValues, whereClause, whereValues); 
       break; 

     case MATCHER_TASK: 
       String id = uri.getPathSegments().get(TasksTable.TASK_ID_PATH_POSITION); 
       finalWhere = TasksTable._ID + " = " + id; 

       // if we were passed a 'where' arg, add that to our 'finalWhere' 
       if (whereClause != null) { 
        finalWhere = finalWhere + " AND " + whereClause; 
       } 
       updatedRowsCount = db.update(TasksTable.TABLE_NAME, updateValues, finalWhere, whereValues); 
       break; 

     default: 
       // Incoming URI pattern is invalid: halt & catch fire. 
       throw new IllegalArgumentException("Unknown URI " + uri); 
     } 
    } finally { 
     db.endTransaction(); 
    } 

    if (updatedRowsCount > 0) { 
     DVSApplication.getContext().getContentResolver().notifyChange(uri, null); 
    } 

    return updatedRowsCount; 
} 

查詢方法:

@Override 
    public Cursor query(Uri uri, String[] selectedColumns, String whereClause, String[] whereValues, String sortOrder) { 

     SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 

     // Choose the projection and adjust the "where" clause based on URI pattern-matching. 
     switch (uriMatcher.match(uri)) { 
     case MATCHER_TASKS: 
      qb.setTables(TasksTable.TABLE_NAME); 
      qb.setProjectionMap(tasksProjection); 
      break; 

     // asking for a single comic - use the rage comics projection, but add a where clause to only return the one 
     // comic 
     case MATCHER_TASK: 
      qb.setTables(TasksTable.TABLE_NAME); 
      qb.setProjectionMap(tasksProjection); 

      // Find the comic ID itself in the incoming URI 
      String taskId = uri.getPathSegments().get(TasksTable.TASK_ID_PATH_POSITION); 
      qb.appendWhere(TasksTable._ID + "=" + taskId); 
      break; 
     case MATCHER_TASK_COMMENTS: 
      qb.setTables(TaskCommentsTable.TABLE_NAME); 
      qb.setProjectionMap(taskCommentsProjection); 
      break; 

     case MATCHER_TASK_COMMENT: 
      qb.setTables(TaskCommentsTable.TABLE_NAME); 
      qb.setProjectionMap(taskCommentsProjection); 

      String commentId = uri.getPathSegments().get(TaskCommentsTable.TASK_COMMENT_ID_PATH_POSITION); 
      qb.appendWhere(TaskCommentsTable._ID + "=" + commentId); 
      break; 
     default: 
      // If the URI doesn't match any of the known patterns, throw an exception. 
      throw new IllegalArgumentException("Unknown URI " + uri); 
     } 

     SQLiteDatabase db = TasksContentProvider.dbHelper.getReadableDatabase(); 
     // the two nulls here are 'grouping' and 'filtering by group' 
     Cursor cursor = qb.query(db, selectedColumns, whereClause, whereValues, null, null, sortOrder); 

     // Tell the Cursor about the URI to watch, so it knows when its source data changes 
     cursor.setNotificationUri(DVSApplication.getContext().getContentResolver(), uri); 

     return cursor; 
    } 

嘗試更新和行。

int affectedRowsCount = provider.update(Uri.parse(TasksTable.CONTENT_URI.toString() + "/"+ taskId), task.getContentValues(), null, null); 

affectedRowsCount是eqaul 1

檢查更新行時

Cursor cs = provider.query(TasksTable.CONTENT_URI, new String[] {TasksTable.TASK_STATE_VALUE}, TasksTable._ID +" = ?", new String[] {String.valueOf(taskId)}, null); 
if(cs.moveToFirst()) { 
    String state = cs.getString(cs.getColumnIndex(TasksTable.TASK_STATE_VALUE)); 
} 

state是一樣的更新之前。雖然更新成功,因爲affectedRowsCount等於1,但通過相同的id選擇同一行似乎該行根本沒有更新。

回答

1

在您的update方法中,您使用的是transaction,但您從未將結果設置爲成功,因此每次達到db.endTransaction()時都會執行rollback。這就是爲什麼你的更新沒有存儲。

如果任何事務結束而沒有 被標記爲乾淨(通過調用setTransactionSuccessful),則更改將回滾。否則 他們將承諾。

您需要使用

db.setTransactionSuccessful(); 

當你更新沒有錯誤完成。在你的代碼中,它應該在你的db.update之後。

相關問題