在Android中通过SQLiteDatabase.delete()中的限制1
//在内容提供商中在Android中通过SQLiteDatabase.delete()中的限制1,android,sqlite,android-contentprovider,Android,Sqlite,Android Contentprovider,//在内容提供商中 public int delete(Uri uri, String selection, String[] selectionArgs) { int uriType = sURIMatcher.match(uri); SQLiteDatabase sqlDB = myDB.getWritableDatabase(); int rowsDeleted = 0; switch (u
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);
有一个student表,我正在根据名称从中删除条目,由于可能有许多同名的学生,我想限制delete函数仅删除第一个条目。请不要告诉我使用学生注册号删除。编译到Android中的SQLite不允许使用
限制子句来删除语句
您必须使用限制1执行选择
(或仅读取第一条返回的记录),并使用学生注册号使用返回的ID进行删除