// IN CONTENT PROVIDER
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;
}
IN CONTENT RESOLVER
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);
有一个学生表,我正在根据名称删除条目,因为可能有许多同名学生我想限制删除功能只删除第一个条目。请不要告诉我使用学生注册号删除
答案 0 :(得分:0)
编译到Android中的SQLite不允许LIMIT
语句的DELETE
子句。
您必须使用SELECT
(或只读取第一个返回的记录)执行LIMIT 1
,并使用返回的ID删除使用学生注册号。