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:com.shalzz.attendance.DatabaseHandler.java

public void addUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.insertWithOnConflict(/*from   w w w  . j av  a  2  s.  com*/
            User.TABLE_NAME, null, User.FACTORY.marshal().sap_id(user.sap_id()).name(user.name())
                    .course(user.course()).password(user.password()).asContentValues(),
            SQLiteDatabase.CONFLICT_REPLACE);
}

From source file:org.qeo.android.service.ApplicationSecurityStandalone.java

@Override
public void insertAppInfo() {
    if (mAppName == null) {
        throw new SecurityException("\"appname\" not set in security manifest");
    }//from  w ww  . j  a v a 2s  . co  m
    if (mVersion == -1) {
        throw new SecurityException("\"version\" not set in security manifest");
    }
    SQLiteDatabase db = mService.getDatabase();
    ContentValues values = new ContentValues();
    values.put(TableManifestMeta.C_ID, mUid);
    values.put(TableManifestMeta.C_PKG_NAME, mPkgName);
    values.put(TableManifestMeta.C_APP_NAME, mAppName);
    values.put(TableManifestMeta.C_VERSION, mVersion);
    values.put(TableManifestMeta.C_APP_VERSION, mAppVersion);
    db.insertWithOnConflict(TableManifestMeta.NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    mAppName = null;
    mVersion = 0;
}

From source file:org.qeo.android.service.ApplicationSecurity.java

/**
 * Insert manifest appinfo block the database. Clear the mAppName and mVersion when everything is saved in the
 * database.//from  ww w . jav  a  2 s  . co  m
 */
public void insertAppInfo() {
    if (mAppName == null) {
        throw new SecurityException("\"appname\" not set in security manifest");
    }
    if (mVersion == -1) {
        throw new SecurityException("\"version\" not set in security manifest");
    }
    SQLiteDatabase db = mService.getDatabase();
    ContentValues values = new ContentValues();
    values.put(TableManifestMeta.C_ID, mUid);
    values.put(TableManifestMeta.C_PKG_NAME, mPkgName);
    values.put(TableManifestMeta.C_APP_NAME, mAppName);
    values.put(TableManifestMeta.C_VERSION, mVersion);
    values.put(TableManifestMeta.C_APP_VERSION, mAppVersion);
    db.insertWithOnConflict(TableManifestMeta.NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    mAppName = null;
    mVersion = 0;
}

From source file:org.tomahawk.libtomahawk.database.DatabaseHelper.java

/**
 * Rename the given {@link Playlist}/*from   ww  w.j a v  a2s.c om*/
 *
 * @param playlist the given {@link Playlist}
 * @param newName  the new playlist name
 */
public void renamePlaylist(final Playlist playlist, final String newName) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (this) {
                ContentValues values = new ContentValues();
                values.put(TomahawkSQLiteHelper.PLAYLISTS_COLUMN_NAME, newName);
                String insertId = playlist.getId();
                values.put(TomahawkSQLiteHelper.PLAYLISTS_COLUMN_CURRENTREVISION,
                        playlist.getCurrentRevision());
                values.put(TomahawkSQLiteHelper.PLAYLISTS_COLUMN_ID, insertId);

                mDatabase.beginTransaction();
                mDatabase.insertWithOnConflict(TomahawkSQLiteHelper.TABLE_PLAYLISTS, null, values,
                        SQLiteDatabase.CONFLICT_REPLACE);
                mDatabase.setTransactionSuccessful();
                mDatabase.endTransaction();
                sendReportResultsBroadcast(playlist.getId());
            }
        }
    }).start();
}

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

@Override
public int bulkInsert(final Uri uri, @NonNull final ContentValues[] valuesArray) {
    try {// ww w . ja  v a 2 s  .c om
        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 0;
        }
        int result = 0;
        final long[] newIds = new long[valuesArray.length];
        if (table != null) {
            mDatabaseWrapper.beginTransaction();
            if (tableId == TABLE_ID_CACHED_USERS) {
                for (final ContentValues values : valuesArray) {
                    final Expression where = Expression.equals(CachedUsers.USER_ID,
                            values.getAsLong(CachedUsers.USER_ID));
                    mDatabaseWrapper.update(table, values, where.getSQL(), null);
                    newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                            SQLiteDatabase.CONFLICT_REPLACE);
                }
            } else if (tableId == TABLE_ID_SEARCH_HISTORY) {
                for (final ContentValues values : valuesArray) {
                    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);
                    newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                            SQLiteDatabase.CONFLICT_IGNORE);
                }
            } else if (shouldReplaceOnConflict(tableId)) {
                for (final ContentValues values : valuesArray) {
                    newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                            SQLiteDatabase.CONFLICT_REPLACE);
                }
            } else {
                for (final ContentValues values : valuesArray) {
                    newIds[result++] = mDatabaseWrapper.insert(table, null, values);
                }
            }
            mDatabaseWrapper.setTransactionSuccessful();
            mDatabaseWrapper.endTransaction();
        }
        if (result > 0) {
            onDatabaseUpdated(tableId, uri);
        }
        onNewItemsInserted(uri, tableId, valuesArray, newIds);
        return result;
    } catch (final SQLException e) {
        Crashlytics.logException(e);
        throw new IllegalStateException(e);
    }
}

From source file:de.vanita5.twittnuker.provider.TwidereDataProvider.java

@Override
public int bulkInsert(final Uri uri, @NonNull final ContentValues[] valuesArray) {
    try {//from  w w  w . j av a2  s  .  co  m
        final int tableId = getTableId(uri);
        final String table = getTableNameById(tableId);
        switch (tableId) {
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
        case TABLE_ID_DIRECT_MESSAGES:
        case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES:
            return 0;
        }
        int result = 0;
        final long[] newIds = new long[valuesArray.length];
        if (table != null) {
            mDatabaseWrapper.beginTransaction();
            if (tableId == TABLE_ID_CACHED_USERS) {
                for (final ContentValues values : valuesArray) {
                    final Expression where = Expression.equals(CachedUsers.USER_ID,
                            values.getAsLong(CachedUsers.USER_ID));
                    mDatabaseWrapper.update(table, values, where.getSQL(), null);
                    newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                            SQLiteDatabase.CONFLICT_IGNORE);
                }
            } else if (tableId == TABLE_ID_SEARCH_HISTORY) {
                for (final ContentValues values : valuesArray) {
                    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);
                    newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                            SQLiteDatabase.CONFLICT_IGNORE);
                }
            } else if (shouldReplaceOnConflict(tableId)) {
                for (final ContentValues values : valuesArray) {
                    newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null, values,
                            SQLiteDatabase.CONFLICT_REPLACE);
                }
            } else {
                for (final ContentValues values : valuesArray) {
                    newIds[result++] = mDatabaseWrapper.insert(table, null, values);
                }
            }
            mDatabaseWrapper.setTransactionSuccessful();
            mDatabaseWrapper.endTransaction();
        }
        if (result > 0) {
            onDatabaseUpdated(tableId, uri);
        }
        onNewItemsInserted(uri, tableId, valuesArray, newIds);
        return result;
    } catch (final SQLException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.shalzz.attendance.DatabaseHandler.java

public void addPeriod(Period period, long timestamp) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.insertWithOnConflict(Period.TABLE_NAME, null,
            Period.FACTORY.marshal().id(period.id()).name(period.name()).week_day(period.week_day())
                    .teacher(Miscellaneous.capitalizeString(period.teacher())).room(period.room().trim())
                    .start_time(period.start_time()).end_time(period.end_time()).batch(period.batch())
                    .last_updated(timestamp).asContentValues(),
            SQLiteDatabase.CONFLICT_REPLACE);
}

From source file:at.bitfire.davdroid.ui.setup.AccountDetailsFragment.java

protected long insertService(SQLiteDatabase db, String accountName, String service,
        DavResourceFinder.Configuration.ServiceInfo info) {
    ContentValues values = new ContentValues();

    // insert service
    values.put(Services.ACCOUNT_NAME, accountName);
    values.put(Services.SERVICE, service);
    if (info.principal != null)
        values.put(Services.PRINCIPAL, info.principal.toString());
    long serviceID = db.insertWithOnConflict(Services._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);

    // insert home sets
    for (URI homeSet : info.homeSets) {
        values.clear();/*  www.  j a v  a2  s  .com*/
        values.put(HomeSets.SERVICE_ID, serviceID);
        values.put(HomeSets.URL, homeSet.toString());
        db.insertWithOnConflict(HomeSets._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    }

    // insert collections
    for (CollectionInfo collection : info.collections.values()) {
        values = collection.toDB();
        values.put(Collections.SERVICE_ID, serviceID);
        db.insertWithOnConflict(Collections._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    }

    return serviceID;
}

From source file:org.ciasaboark.tacere.database.DatabaseInterface.java

public void insertEvent(EventInstance e) {
    if (!isEventValidToInsert(e)) {
        throw new IllegalArgumentException("DatabaseInterface:insertEvent given an event with blank values");
    }/*from w ww  .j a va  2s.c o m*/

    ContentValues cv = new ContentValues();
    cv.put(Columns._ID, e.getId());
    cv.put(Columns.TITLE, e.getTitle());
    cv.put(Columns.BEGIN, e.getBegin());
    cv.put(Columns.ORGINAL_END, e.getOriginalEnd());
    cv.put(Columns.DESCRIPTION, e.getDescription());
    cv.put(Columns.IS_ALLDAY, e.isAllDay());
    cv.put(Columns.IS_FREETIME, e.isFreeTime());
    cv.put(Columns.RINGER_TYPE, e.getRingerType().value);
    cv.put(Columns.DISPLAY_COLOR, e.getDisplayColor());
    cv.put(Columns.CAL_ID, e.getCalendarId());
    cv.put(Columns.EVENT_ID, e.getEventId());
    cv.put(Columns.LOCATION, e.getLocation());
    cv.put(Columns.EXTEND_BUFFER, e.getExtendMinutes());
    cv.put(Columns.EFFECTIVE_END, e.getEffectiveEnd());

    long rowID = eventsDB.insertWithOnConflict(EventDatabaseOpenHelper.TABLE_EVENTS, null, cv,
            SQLiteDatabase.CONFLICT_REPLACE);
    Log.d(TAG, "inserted event " + e.toString() + " as row " + rowID);
}

From source file:de.vanita5.twittnuker.provider.TwidereDataProvider.java

@Override
public Uri insert(final Uri uri, final ContentValues values) {
    try {/*from  ww w  . j ava2  s  .c  o  m*/
        final int tableId = getTableId(uri);
        final String table = getTableNameById(tableId);
        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) {
        throw new IllegalStateException(e);
    }
}