Code example for ContentResolver

Methods: deleteinsert

0
                values.put(SuggestionColumns.DISPLAY2, line2);
            } 
            values.put(SuggestionColumns.QUERY, queryString);
            values.put(SuggestionColumns.DATE, now);
            values.put(SuggestionColumns.AUTHORITY, mAuthority);
            cr.insert(mSuggestionsUri, values);
        } catch (RuntimeException e) {
            Log.e(TAG, "saveRecentQuery", e);
        } 
         
        // Shorten the list (if it has become too long) 
        truncateHistory(cr, MAX_HISTORY_COUNT);
    } 
 
    protected void truncateHistory(ContentResolver cr, int maxEntries) {
        if (maxEntries < 0) {
            throw new IllegalArgumentException();
        } 
         
        try { 
            // null means "delete all".  otherwise "delete but leave n newest" 
            String selection = null;
            if (maxEntries > 0) {
                selection = SuggestionColumns._ID + " IN " +
                        "(SELECT " + SuggestionColumns._ID +
                        " FROM " + TABLE_NAME +
                        " WHERE " + SuggestionColumns.AUTHORITY + " LIKE '" + mAuthority + "'" +
                        " ORDER BY " + SuggestionColumns.DATE + " DESC" +
                        " LIMIT -1 OFFSET " + String.valueOf(maxEntries) + ")";
            } 
            cr.delete(mSuggestionsUri, selection, null);
        } catch (RuntimeException e) {
            Log.e(TAG, "truncateHistory", e);
        } 
    }