0

//在内容提供者中

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);

有一个学生表,我从中删除基于名称的条目,并且由于可能有许多同名的学生,我想将删除功能限制为仅删除第一个条目。请不要告诉我使用学生注册号删除。

4

1 回答 1

0

编译到 Android 中的 SQLite 不允许语句的LIMIT子句DELETE

您必须SELECT使用 a LIMIT 1(或只读返回的第一条记录),并使用返回的 ID 使用学生注册号进行删除。

于 2013-11-14T08:43:05.983 回答