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

时间:2018-08-13 22:41:55

标签: android android-sqlite android-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的项目。我错过了什么?

1 个答案:

答案 0 :(得分:1)

尝试一下:

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

在您的delete的{​​{1}}方法内,删除contentProvider,您的case FLIGHT_RESERVATION_WITH_ID:将如下所示:

case  FLIGHT_RESERVATIONS: