尝试通过 contentProvider 从 sqlite 中删除有条件的行

Trying Delete row with condition from sqlite by contentProvider

我正在开发 android 预订应用程序,我需要将 SQLite 与内容提供程序一起使用。 我现在正在尝试根据他的 ID(使用 where 条件)删除行..但是删除不起作用..

这是我的内容提供商删除代码:

    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {

        // Get access to the database and write URI matching code to recognize a single item
        final SQLiteDatabase db = dbHelper.getWritableDatabase();

        int match = sUrimatcher.match(uri);
        // Keep track of the number of deleted tasks
        int tasksDeleted; // starts as 0

        // Write the code to delete a single row of data
        // [Hint] Use selections to delete an item by its row ID
        switch (match) {
            // Handle the single item case, recognized by the ID included in the URI path
            case USER_WITH_ID:{
                // Get the task ID from the URI path
                String id = uri.getPathSegments().get(1);
                // Use selections/selectionArgs to filter for this ID
                tasksDeleted = db.delete(DSHContract.UserEntry.TABLE_NAME, "_id=?", new String[]{id});
                break;
            }
            case  USERS_:
            {
                tasksDeleted = db.delete(DSHContract.UserEntry.TABLE_NAME,null,null);
                break;
            }
            case  FLIGHT_RESERVATIONS:
            {
                tasksDeleted = db.delete(DSHContract.FlightReservationsEntry.TABLE_NAME,null,null);
                break;
            }
            case FLIGHT_RESERVATION_WITH_ID:
                {
                // Get the task ID from the URI path
                String id = uri.getPathSegments().get(1);
                // Use selections/selectionArgs to filter for this ID
                tasksDeleted = db.delete(DSHContract.FlightReservationsEntry.TABLE_NAME, "_id=?", new String[]{id});
                break;
            }
            case  HOTEL_RESERVATIONS:
            {
                tasksDeleted = db.delete(DSHContract.HotelReservationsEntry.TABLE_NAME,null,null);
                break;
            }
            case HOTEL_RESERVATION_WITH_ID:{
                // Get the task ID from the URI path
                String id = uri.getPathSegments().get(1);
                // Use selections/selectionArgs to filter for this ID
                tasksDeleted = db.delete(DSHContract.HotelReservationsEntry.TABLE_NAME, "_id=?", new String[]{id});
                break;}
            default:
                throw new UnsupportedOperationException("Unknown uri DELETE: " + uri);
        }

        // Notify the resolver of a change and return the number of items deleted
        if (tasksDeleted != 0) {
            // A task was deleted, set notification
            getContext().getContentResolver().notifyChange(uri, null);
        }


        // Return the number of tasks deleted
        return tasksDeleted;
    }

这是我根据 contentProvider 尝试删除的内容:

String stringId = String.valueOf(referanceId);
                                        Uri uri = DSHContract.FlightReservationsEntry.CONTENT_URI;
                                        uri = uri.buildUpon().appendPath(stringId).build();

                                        getContentResolver().delete(uri, null, null);
                                      //  getContentResolver().delete(DSHContract.FlightReservationsEntry.CONTENT_URI, referanceId,null);
                                       // getContentResolver().query(DSHContract.UserEntry.CONTENT_URI, USER_COLUMNS,"reservation_id='"+referanceId+"'",null,null,null);

我只需要使用 contentProvider 删除带有他的 ID 的项目。我错过了什么??

试试这个:

 case FLIGHT_RESERVATION_WITH_ID:
            {
            // Get the task ID from the URI path

            String id = uri.getLastPathSegment();

            // Use selections/selectionArgs to filter for this ID
            tasksDeleted = db.delete(DSHContract.FlightReservationsEntry.TABLE_NAME, "_id = ? ", new String[]{id});
            break;
        }

或者你可以这样做:

String stringId = String.valueOf(referanceId);
Uri uri = DSHContract.FlightReservationsEntry.CONTENT_URI;    
getContentResolver().delete(uri, "_id = ? ", new String[]{stringId});

并在 contentProviderdelete 方法中删除 case FLIGHT_RESERVATION_WITH_ID: 并且您的 case FLIGHT_RESERVATIONS: 将如下所示:

case  FLIGHT_RESERVATIONS:{
  tasksDeleted = db.delete(DSHContract.FlightReservationsEntry.TABLE_NAME, selection, selectionArgs);
  break;
}