Android 内容提供程序中的更新函数不更新数据库
我正在努力使更新方法在我的内容提供商中工作,更新返回0,并且表中没有更新的信息。此时将填充该表 以下是更新功能:Android 内容提供程序中的更新函数不更新数据库,android,android-contentprovider,Android,Android Contentprovider,我正在努力使更新方法在我的内容提供商中工作,更新返回0,并且表中没有更新的信息。此时将填充该表 以下是更新功能: @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = leagueDBHelper.getWritableDatabase(); int rowsUpdated = 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);
使用连接到数据库并尝试手动更新
您还可以在内容提供商中添加日志消息以跟踪值。我觉得一切正常。你查过玩家id了吗?是的,它从数据库中提取分数。所以player1Id是正确的。我觉得一切都很好,真的看不出问题!问题解决了,列_KEY _ID是一个整数,使用了列的标题,这样就行了。