【问题标题】:Trying Delete row with condition from sqlite by contentProvider尝试通过 contentProvider 从 sqlite 中删除有条件的行
【发布时间】:2018-08-13 22:41:54
【问题描述】:

我正在开发 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 的项目。我错过了什么??

【问题讨论】:

    标签: android android-sqlite android-contentprovider


    【解决方案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});
    

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

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

    【讨论】:

    • 非常感谢!现在通过删除带有 ID 的案例来工作,并在我的活动中使用此方法:getContentResolver().delete(DSHContract.FlightReservationsEntry.CONTENT_URI, "reservation_number='"+referanceId+"'",null);
    猜你喜欢
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2013-08-08
    • 2022-09-28
    相关资源
    最近更新 更多