Example usage for android.database.sqlite SQLiteDatabase CONFLICT_IGNORE

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

Introduction

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

Prototype

int CONFLICT_IGNORE

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

Click Source Link

Document

When a constraint violation occurs, the one row that contains the constraint violation is not inserted or changed.

Usage

From source file:com.cyanogenmod.eleven.provider.LocalizedStore.java

private void updateSongData(SQLiteDatabase db, long id, String name, long artistId, long albumId) {
    mContentValues.clear();/*from  ww w . j ava2 s.c o  m*/
    name = MusicUtils.getTrimmedName(name);

    final LocaleUtils localeUtils = LocaleUtils.getInstance();
    final int bucketIndex = localeUtils.getBucketIndex(name);

    mContentValues.put(SongSortColumns.ID, id);
    mContentValues.put(SongSortColumns.NAME, name);
    mContentValues.put(SongSortColumns.NAME_BUCKET, bucketIndex);
    mContentValues.put(SongSortColumns.NAME_LABEL, localeUtils.getBucketLabel(bucketIndex));
    mContentValues.put(SongSortColumns.ARTIST_ID, artistId);
    mContentValues.put(SongSortColumns.ALBUM_ID, albumId);

    db.insertWithOnConflict(SongSortColumns.TABLE_NAME, null, mContentValues, SQLiteDatabase.CONFLICT_IGNORE);
}

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

@Override
public Uri insert(final Uri uri, final ContentValues values) {
    try {//from   w ww.  java  2  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);
    }
}

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

@Override
public Uri insert(final Uri uri, final ContentValues values) {
    try {/*from  w  w w.  j av  a 2 s  .  com*/
        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.concentricsky.android.khanacademy.data.remote.LibraryUpdaterTask.java

private void insertTopicVideo(SQLiteDatabase tempDb, ContentValues values) {
    // This values is for a video.
    ContentValues v = new ContentValues();
    v.put("topic_id", values.getAsString("parentTopic_id"));
    v.put("video_id", values.getAsString("readable_id"));
    tempDb.insertWithOnConflict(topicvideoTableName, null, v, SQLiteDatabase.CONFLICT_IGNORE);
}

From source file:com.concentricsky.android.khanacademy.data.remote.LibraryUpdaterTask.java

private void insertVideo(SQLiteDatabase tempDb, ContentValues values) {
    tempDb.insertWithOnConflict(videoTableName, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}

From source file:net.potterpcs.recipebook.RecipeData.java

public void insertIngredients(ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {/*from   ww w  . j  av  a  2s . c o m*/
            db.insertWithOnConflict(INGREDIENTS_TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        } finally {
            db.close();
        }
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public void insertDirections(ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {//ww w.j  ava  2  s .c o m
            db.insertWithOnConflict(DIRECTIONS_TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        } finally {
            db.close();
        }
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public void insertTags(ContentValues values) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {/*from   w  w w.j av  a  2s  .  c  o m*/
            db.insertWithOnConflict(TAGS_TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        } finally {
            db.close();
        }
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public int insertRecipe(Recipe r) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int ret = -1;
        try {// w ww. j  a v a  2 s.c  om
            ContentValues values = createRecipeForInsert(r);
            long rowid = db.insert(RECIPES_TABLE, null, values);

            if (r.ingredients != null) {
                for (String ing : r.ingredients) {
                    ContentValues cvi = createIngredientsCV(rowid, ing);
                    db.insertWithOnConflict(INGREDIENTS_TABLE, null, cvi, SQLiteDatabase.CONFLICT_IGNORE);
                }
            }

            if (r.directions != null) {
                int step = 1;
                for (String dir : r.directions) {
                    ContentValues cdirs = createDirectionsCV(rowid, step, dir, r.directions_photos[step - 1]);
                    db.insertWithOnConflict(DIRECTIONS_TABLE, null, cdirs, SQLiteDatabase.CONFLICT_IGNORE);
                    step++;
                }
            }

            if (r.tags != null) {
                for (String tag : r.tags) {
                    ContentValues ctags = createTagsCV(rowid, tag);
                    db.insertWithOnConflict(TAGS_TABLE, null, ctags, SQLiteDatabase.CONFLICT_IGNORE);
                }
            }
            ret = (int) rowid;
        } finally {
            db.close();
        }
        return ret;
    }
}

From source file:net.potterpcs.recipebook.RecipeData.java

public int updateRecipe(Recipe r) {
    synchronized (DB_LOCK) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int ret = -1;
        try {//from   w  ww.ja va  2  s . co  m
            long rid = r.id;
            String[] whereArgs = { Long.toString(rid) };
            ret = db.update(RECIPES_TABLE, createRecipeForInsert(r), RT_ID + " = ?",
                    new String[] { Long.toString(r.id) });

            // TODO until we can figure out a smarter way to update
            db.delete(INGREDIENTS_TABLE, IT_RECIPE_ID + " = ?", whereArgs);
            for (String ing : r.ingredients) {
                db.insertWithOnConflict(INGREDIENTS_TABLE, null, createIngredientsCV(rid, ing),
                        SQLiteDatabase.CONFLICT_IGNORE);
            }

            db.delete(DIRECTIONS_TABLE, DT_RECIPE_ID + " = ?", whereArgs);
            int step = 1;
            for (String dir : r.directions) {
                db.insertWithOnConflict(DIRECTIONS_TABLE, null,
                        createDirectionsCV(rid, step, dir, r.directions_photos[step - 1]),
                        SQLiteDatabase.CONFLICT_IGNORE);
                step++;
            }

            db.delete(TAGS_TABLE, TT_RECIPE_ID + " = ?", whereArgs);
            for (String tag : r.tags) {
                db.insertWithOnConflict(TAGS_TABLE, null, createTagsCV(rid, tag),
                        SQLiteDatabase.CONFLICT_IGNORE);
            }
        } finally {
            db.close();
        }
        return ret;
    }
}