//在内容提供者中
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = myDB.getWritableDatabase();
int rowsDeleted = 0;
switch (uriType) {
case STUDENTS:
// rowsDeleted = sqlDB.delete(MyDBHandler.TABLE_STUDENTS, selection, selectionArgs);
rowsDeleted = sqlDB.delete(MyDBHandler.TABLE_STUDENTS, selection + " ?", new String [] {"limit 1"});
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
在内容解析器中
private ContentProviderClient myCR;
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/"+ STUDENT_TABLE);
public MyDBHandler(Context context, String name, CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
myCR =
context.getContentResolver().acquireContentProviderClient(CONTENT_URI);
public boolean deleteStudent(String studentName) {
String selection = "sName = " + studentName + " LIMIT 1";
rowsDeleted = myCR.delete(CONTENT_URI, selection,null);
//also tried this
String selection = "sName = " + studentName ;
rowsDeleted = myCR.delete(CONTENT_URI.buildUpon().encodedQuery("limit 1").build(), selection,null);
有一个学生表,我从中删除基于名称的条目,并且由于可能有许多同名的学生,我想将删除功能限制为仅删除第一个条目。请不要告诉我使用学生注册号删除。