Example usage for android.database.sqlite SQLiteDatabase insertWithOnConflict

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

Introduction

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

Prototype

public long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues,
        int conflictAlgorithm) 

Source Link

Document

General method for inserting a row into the database.

Usage

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

private void updateArtistData(SQLiteDatabase db, long id, String name) {
    mContentValues.clear();// www  .  j  a  v  a2 s  .c om
    name = MusicUtils.getTrimmedName(name);

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

    mContentValues.put(ArtistSortColumns.ID, id);
    mContentValues.put(ArtistSortColumns.NAME, name);
    mContentValues.put(ArtistSortColumns.NAME_BUCKET, bucketIndex);
    mContentValues.put(ArtistSortColumns.NAME_LABEL, localeUtils.getBucketLabel(bucketIndex));

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

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

private void updateAlbumData(SQLiteDatabase db, long id, String name, long artistId) {
    mContentValues.clear();// w w  w.ja  va2s  .c o  m
    name = MusicUtils.getTrimmedName(name);

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

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

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

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

private void updateSongData(SQLiteDatabase db, long id, String name, long artistId, long albumId) {
    mContentValues.clear();/* ww  w.j  a va  2s  .  c  om*/
    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:cn.edu.wyu.documentviewer.RecentsProvider.java

@Override
public Uri insert(Uri uri, ContentValues values) {
    final SQLiteDatabase db = mHelper.getWritableDatabase();
    final ContentValues key = new ContentValues();
    switch (sMatcher.match(uri)) {
    case URI_RECENT:
        values.put(RecentColumns.TIMESTAMP, System.currentTimeMillis());
        db.insert(TABLE_RECENT, null, values);
        final long cutoff = System.currentTimeMillis() - MAX_HISTORY_IN_MILLIS;
        db.delete(TABLE_RECENT, RecentColumns.TIMESTAMP + "<" + cutoff, null);
        return uri;
    case URI_STATE:
        final String authority = uri.getPathSegments().get(1);
        final String rootId = uri.getPathSegments().get(2);
        final String documentId = uri.getPathSegments().get(3);

        key.put(StateColumns.AUTHORITY, authority);
        key.put(StateColumns.ROOT_ID, rootId);
        key.put(StateColumns.DOCUMENT_ID, documentId);

        // Ensure that row exists, then update with changed values
        db.insertWithOnConflict(TABLE_STATE, null, key, SQLiteDatabase.CONFLICT_IGNORE);
        db.update(TABLE_STATE, values, StateColumns.AUTHORITY + "=? AND " + StateColumns.ROOT_ID + "=? AND "
                + StateColumns.DOCUMENT_ID + "=?", new String[] { authority, rootId, documentId });

        return uri;
    case URI_RESUME:
        values.put(ResumeColumns.TIMESTAMP, System.currentTimeMillis());

        final String packageName = uri.getPathSegments().get(1);
        key.put(ResumeColumns.PACKAGE_NAME, packageName);

        // Ensure that row exists, then update with changed values
        db.insertWithOnConflict(TABLE_RESUME, null, key, SQLiteDatabase.CONFLICT_IGNORE);
        db.update(TABLE_RESUME, values, ResumeColumns.PACKAGE_NAME + "=?", new String[] { packageName });
        return uri;
    default:/*from ww  w.  j ava2s.  com*/
        throw new UnsupportedOperationException("Unsupported Uri " + uri);
    }
}

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  w w . j  a v a 2  s.  co m*/
            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 {/*w ww.  j a va  2s  . c o  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;
    }
}

From source file:com.sina.weibo.sdk.demo.sample.activity.TestFragment.java

public void saveData(ArrayList<Status> statusList) {
    SQLiteDatabase databaseHelper = Utils.getDatabaseHelper().getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    for (Status status : statusList) {
        //            contentValues.put("id", Long.parseLong(status.idstr));
        contentValues.put("name", status.user.name);
        contentValues.put("gender", status.user.gender);
        contentValues.put("location", status.user.location);
        contentValues.put("description", status.user.description);
        contentValues.put("followers_count", status.user.followers_count);
        contentValues.put("friends_count", status.user.friends_count);
        contentValues.put("statuses_count", status.user.statuses_count);
        contentValues.put("main_content", status.text);
        contentValues.put("created_at", status.created_at);
        contentValues.put("source", status.source);
        if (status.retweeted_status != null) {
            contentValues.put("sub_status", status.retweeted_status != null ? 1 : 0);
            contentValues.put("sub_name", status.retweeted_status.user.name);
            contentValues.put("sub_content", status.retweeted_status.text);
        }/*from w  w  w .  j  a v  a2s.c  om*/
    }
    databaseHelper.insertWithOnConflict("Status", null, contentValues, SQLiteDatabase.CONFLICT_IGNORE);
    databaseHelper.close();

}

From source file:com.xengar.android.englishverbs.data.VerbDBHelper.java

/**
 * Insert default verbs./*from   ww w. j  a  va2  s.  c  o  m*/
 * @param db SQLiteDatabase
 */
private void insertVerbs(SQLiteDatabase db) {
    ContentValues values = new ContentValues();
    final String DEFAULT_COLOR = "" + ContextCompat.getColor(context, R.color.colorBlack);
    final String DEFAULT_SCORE = "0";
    final String DEFAULT_SOURCE = "0"; // application
    for (int i = 0; i < verbs.length; i++) {
        values.put("_id", i);
        values.put(VerbEntry.COLUMN_ID, verbs[i][0]);
        values.put(VerbEntry.COLUMN_INFINITIVE, verbs[i][1]);
        values.put(VerbEntry.COLUMN_SIMPLE_PAST, verbs[i][2]);
        values.put(VerbEntry.COLUMN_PAST_PARTICIPLE, verbs[i][3]);
        values.put(VerbEntry.COLUMN_PHONETIC_INFINITIVE, verbs[i][4]);
        values.put(VerbEntry.COLUMN_PHONETIC_SIMPLE_PAST, verbs[i][5]);
        values.put(VerbEntry.COLUMN_PHONETIC_PAST_PARTICIPLE, verbs[i][6]);
        values.put(VerbEntry.COLUMN_SAMPLE_1, verbs[i][7]);
        values.put(VerbEntry.COLUMN_SAMPLE_2, verbs[i][8]);
        values.put(VerbEntry.COLUMN_SAMPLE_3, verbs[i][9]);
        values.put(VerbEntry.COLUMN_COMMON, verbs[i][10]);
        values.put(VerbEntry.COLUMN_REGULAR, verbs[i][11]);
        values.put(VerbEntry.COLUMN_COLOR, DEFAULT_COLOR);
        values.put(VerbEntry.COLUMN_SCORE, DEFAULT_SCORE);
        values.put(VerbEntry.COLUMN_SOURCE, DEFAULT_SOURCE);
        values.put(VerbEntry.COLUMN_DEFINITION, verbs[i][12]);
        values.put(VerbEntry.COLUMN_TRANSLATION_ES, verbs[i][13]);
        values.put(VerbEntry.COLUMN_TRANSLATION_FR, verbs[i][14]);
        values.put(VerbEntry.COLUMN_NOTES, verbs[i][15]);
        db.insertWithOnConflict(VerbEntry.VERBS_TBL, null, values, CONFLICT_REPLACE);
    }
}

From source file:com.rener.sea.DBHelper.java

private long setCategory(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//  w ww.  ja v a2s  .  com
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);

            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.CATEGORY_ID))
                values.put(DBSchema.CATEGORY_ID, item.getLong(DBSchema.CATEGORY_ID));
            if (!item.isNull(DBSchema.CATEGORY_NAME))
                values.put(DBSchema.CATEGORY_NAME, item.getString(DBSchema.CATEGORY_NAME));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);

            db.insertWithOnConflict(DBSchema.TABLE_CATEGORY, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

From source file:com.rener.sea.DBHelper.java

private long setSpecialization(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;/*ww w  . j  a v  a2s . c o m*/
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            ContentValues values = new ContentValues();
            if (!item.isNull(DBSchema.SPECIALIZATION_ID))
                values.put(DBSchema.SPECIALIZATION_ID, item.getLong(DBSchema.SPECIALIZATION_ID));
            if (!item.isNull(DBSchema.SPECIALIZATION_NAME))
                values.put(DBSchema.SPECIALIZATION_NAME, item.getString(DBSchema.SPECIALIZATION_NAME));
            values.put(DBSchema.MODIFIED, DBSchema.MODIFIED_NO);
            db.insertWithOnConflict(DBSchema.TABLE_SPECIALIZATION, null, values, 5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;

}