Example usage for android.database.sqlite SQLiteDatabase CONFLICT_REPLACE

List of usage examples for android.database.sqlite SQLiteDatabase CONFLICT_REPLACE

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase CONFLICT_REPLACE.

Prototype

int CONFLICT_REPLACE

To view the source code for android.database.sqlite SQLiteDatabase CONFLICT_REPLACE.

Click Source Link

Document

When a UNIQUE constraint violation occurs, the pre-existing rows that are causing the constraint violation are removed prior to inserting or updating the current row.

Usage

From source file:org.getlantern.firetweet.provider.FiretweetDataProvider.java

@Override
public Uri insert(final Uri uri, final ContentValues values) {
    try {//ww  w .j a va  2s  .  c  o  m
        final int tableId = getTableId(uri);
        final String table = getTableNameById(tableId);
        checkWritePermission(tableId, table);
        switch (tableId) {
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
        case TABLE_ID_DIRECT_MESSAGES:
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES:
            return null;
        }
        if (table == null)
            return null;
        final long rowId;
        if (tableId == TABLE_ID_CACHED_USERS) {
            final Expression where = Expression.equals(CachedUsers.USER_ID,
                    values.getAsLong(CachedUsers.USER_ID));
            mDatabaseWrapper.update(table, values, where.getSQL(), null);
            rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        } else if (tableId == TABLE_ID_SEARCH_HISTORY) {
            values.put(SearchHistory.RECENT_QUERY, System.currentTimeMillis());
            final Expression where = Expression.equalsArgs(SearchHistory.QUERY);
            final String[] args = { values.getAsString(SearchHistory.QUERY) };
            mDatabaseWrapper.update(table, values, where.getSQL(), args);
            rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        } else if (tableId == TABLE_ID_CACHED_RELATIONSHIPS) {
            final long accountId = values.getAsLong(CachedRelationships.ACCOUNT_ID);
            final long userId = values.getAsLong(CachedRelationships.USER_ID);
            final Expression where = Expression.and(
                    Expression.equals(CachedRelationships.ACCOUNT_ID, accountId),
                    Expression.equals(CachedRelationships.USER_ID, userId));
            if (mDatabaseWrapper.update(table, values, where.getSQL(), null) > 0) {
                final String[] projection = { CachedRelationships._ID };
                final Cursor c = mDatabaseWrapper.query(table, projection, where.getSQL(), null, null, null,
                        null);
                if (c.moveToFirst()) {
                    rowId = c.getLong(0);
                } else {
                    rowId = 0;
                }
                c.close();
            } else {
                rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                        SQLiteDatabase.CONFLICT_IGNORE);
            }
        } else if (shouldReplaceOnConflict(tableId)) {
            rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        } else {
            rowId = mDatabaseWrapper.insert(table, null, values);
        }
        onDatabaseUpdated(tableId, uri);
        onNewItemsInserted(uri, tableId, values, rowId);
        return Uri.withAppendedPath(uri, String.valueOf(rowId));
    } catch (final SQLException e) {
        Crashlytics.logException(e);
        throw new IllegalStateException(e);
    }
}

From source file:com.android.quicksearchbox.ShortcutRepositoryImplLog.java

@VisibleForTesting
void refreshShortcut(Source source, final String shortcutId, SuggestionCursor refreshed) {
    if (source == null)
        throw new NullPointerException("source");
    if (shortcutId == null)
        throw new NullPointerException("shortcutId");

    final String[] whereArgs = { shortcutId, source.getName() };
    final ContentValues shortcut;
    if (refreshed == null || refreshed.getCount() == 0) {
        shortcut = null;//from   w  w  w.j  av a 2 s  .  c  om
    } else {
        refreshed.moveTo(0);
        shortcut = makeShortcutRow(refreshed);
    }

    runTransactionAsync(new SQLiteTransaction() {
        @Override
        protected boolean performTransaction(SQLiteDatabase db) {
            if (shortcut == null) {
                if (DBG)
                    Log.d(TAG, "Deleting shortcut: " + shortcutId);
                db.delete(Shortcuts.TABLE_NAME, SHORTCUT_BY_ID_WHERE, whereArgs);
            } else {
                if (DBG)
                    Log.d(TAG, "Updating shortcut: " + shortcut);
                db.updateWithOnConflict(Shortcuts.TABLE_NAME, shortcut, SHORTCUT_BY_ID_WHERE, whereArgs,
                        SQLiteDatabase.CONFLICT_REPLACE);
            }
            return true;
        }
    });
}