内容提供者更新功能不更新数据库

时间:2016-04-17 19:56:54

标签: android android-contentprovider

我正在努力让更新方法在我的内容提供程序中运行,更新返回0并且表中没有更新信息。此时填充表格。

这是更新功能:

   @Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    SQLiteDatabase db = leagueDBHelper.getWritableDatabase();
    int rowsUpdated = 0;
    int uriType = uriMatcher.match(uri);
    switch (uriType) {
        case LEAGUES:
            rowsUpdated = db.update(LeagueContract.LEAGUE_TABLE_NAME,
                    values, selection, selectionArgs);
            break;
        case LEAGUE_ID:
            String newSelection = appendToSelection(uri, selection);
            rowsUpdated = db.update(LeagueContract.LEAGUE_TABLE_NAME,
                    values, newSelection, selectionArgs);

            break;
        default:
            throw new IllegalArgumentException("Unrecognised uri: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri,null);
    return rowsUpdated;

}

private String appendToSelection(Uri uri, String selection) {
    String id = uri.getLastPathSegment();
    StringBuilder newSelection = new StringBuilder(LeagueContract.COLUMN_KEY_ID + "=" + id);
    if (selection != null && !selection.isEmpty()) {
        newSelection.append(" AND " + selection);
    }
    return newSelection.toString();
}

这是我调用内容提供商更新的地方:

     message = "Player 1 wins";// we should get the name
     ContentValues mUpdateValues = new ContentValues();
     String[] projectionFields = new String[] {
     LeagueContract.COLUMN_NAME_SCORE   };
     Uri uri = ContentUris.withAppendedId(LeagueContentProvider.LEAGUE_URI, player1Id);
     ContentResolver content = getContentResolver();
     Cursor  cursor = content.query(uri, projectionFields, null, null, null);
     cursor.moveToFirst();
     int score = cursor.getInt(0);
     score ++;
     ContentValues values = new ContentValues();
     values.put(LeagueContract.COLUMN_NAME_SCORE,score);
     content.update(uri,values,null,null);

1 个答案:

答案 0 :(得分:0)

使用the remote shell连接到您的数据库并尝试手动更新。

您还可以在内容提供商中添加日志消息以跟踪值。