Search code examples
androidandroid-sqliteandroid-contentprovider

Trying Delete row with condition from sqlite by contentProvider


I am working on android booking app, I need to use SQLite with content provider. I am trying now to delete row depending of his Id (with where condition).. but the deletion is not working..

this is my content provider delete code:

    @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;
    }

and this is my trying to delete depending of 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);

All I need is to delete item with his id by using contentProvider.. What I missed??


Solution

  • Try this:

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

    Or you can do something like this:

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

    and inside your delete method of contentProvider remove case FLIGHT_RESERVATION_WITH_ID: and your case FLIGHT_RESERVATIONS: will look like this:

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