Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLite更新后的结果相同_Android_Android Contentprovider_Android Sqlite - Fatal编程技术网

Android SQLite更新后的结果相同

Android SQLite更新后的结果相同,android,android-contentprovider,android-sqlite,Android,Android Contentprovider,Android Sqlite,ContentProvider中SQLite更新的奇怪行为 更新方法: @Override public int update(Uri uri, ContentValues updateValues, String whereClause, String[] whereValues) { SQLiteDatabase db = TasksContentProvider.dbHelper.getWritableDatabase(); int updatedRowsCount;

ContentProvider中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
等于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));
}

状态与更新前相同。虽然更新成功,因为
affectedRowsCount
等于1,但通过相同的id选择相同的行似乎根本没有更新行。

update
方法中,您使用的是
事务
,但您从未将结果设置为成功,因此每次到达
db.endTransaction()
执行
回滚
。这就是为什么没有存储您的更新

如果任何事务结束时没有更改,则更改将回滚 标记为干净(通过调用setTransactionSuccessful)。否则 他们将被承诺

你需要使用

db.setTransactionSuccessful();
当您的更新完成且没有错误时。在代码中,它应该位于
db.update
之后