Java Code Examples for android.database.sqlite.SQLiteDatabase.insertWithOnConflict()

The following are Jave code examples for showing how to use insertWithOnConflict() of the android.database.sqlite.SQLiteDatabase class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: IslamicLibraryAndroid   File: UserDataDBHelper.java   View Source Code Vote up 7 votes
void logBookAccess(int bookId) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValue = new ContentValues();
    contentValue.put(UserDataDBContract.AccessInformationEntry.COLUMN_NAME_BOOK_ID, bookId);
    db.insertWithOnConflict(UserDataDBContract.AccessInformationEntry.Table_NAME,
            null,
            contentValue,
            SQLiteDatabase.CONFLICT_IGNORE);

    db.execSQL(" UPDATE " +
                    UserDataDBContract.AccessInformationEntry.Table_NAME +
                    " SET " + UserDataDBContract.AccessInformationEntry.COLUMN_ACCESS_COUNT + "=" +
                    UserDataDBContract.AccessInformationEntry.COLUMN_ACCESS_COUNT + "+1" +
                    SQL.WHERE + UserDataDBContract.AccessInformationEntry.COLUMN_NAME_BOOK_ID + "=?",
            new String[]{String.valueOf(bookId)});
}
 
Example 2
Project: OpenHomeAnalysis   File: OhaEnergyUseProvider.java   View Source Code Vote up 5 votes
/**
 * Inclusão em bloco dos logs de utilização de energia.
 */
private int bulkInsertEnergyUseLogs(@NonNull Uri uri, @NonNull ContentValues[] values, SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.beginTransaction();
    //Armazenar as datas dos novos logs para atualizar o cache de  utilização de energia.
    Set<Long> setDateUpdated = new TreeSet<>();
    try {
        for (int row = 0; row < values.length; row++) {
            ContentValues value = values[row];
            long insertedResult = sqLiteDatabase.insertWithOnConflict(
                    EnergyUseLogEntry.TABLE_NAME,
                    null,
                    value,
                    SQLiteDatabase.CONFLICT_REPLACE);
            if (insertedResult == -1) {
                throw new SQLException(String.format("The EnergyUseLog content on the row %s It is not valid.", row));
            }
            setDateUpdated.add(value.getAsLong(EnergyUseLogEntry._ID));
        }
        sqLiteDatabase.setTransactionSuccessful();
    } finally {
        sqLiteDatabase.endTransaction();
    }
    //Atualizar o cache de  utilização de energia.
    updateEnergyUseTotalOnCache(setDateUpdated);
    //Notificar a atualização para as Uri relacionadas a tabela EnergyUseLog
    notifyUpdatedEntities(uri);
    return values.length;
}
 
Example 3
Project: Farmacias   File: DbProvider.java   View Source Code Vote up 5 votes
@Override
public int bulkInsert(@NonNull Uri uri, ContentValues[] values) {

    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    final int match = mUriMatcher.match(uri);
    int returnCount = 0;
    switch (match) {
        case FARMACIAS:
            db.beginTransaction();

            try {
                for (ContentValues value : values) {
                    long _id = db.insertWithOnConflict(DbContract.FarmaciasEntity.TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_REPLACE);
                    if (_id != -1) {
                        returnCount++;
                    }
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
            getContext().getContentResolver().notifyChange(uri, null);
            return returnCount;

        default:
            return super.bulkInsert(uri, values);
    }


}
 
Example 4
Project: minu-poska-android   File: TimetableDatabaseHelper.java   View Source Code Vote up 5 votes
public boolean insertTimetablePeriod(int id, String startTime, String endTime, String timetableID) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_PERIOD_ID, id);
    contentValues.put(COL_START_TIME, startTime);
    contentValues.put(COL_END_TIME, endTime);
    contentValues.put(COL_TIMETABLE_ID, timetableID);
    long result = db.insertWithOnConflict(TABLE_PERIODS, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    return result != -1;
}
 
Example 5
Project: RNLearn_Project1   File: AsyncLocalStorageUtil.java   View Source Code Vote up 5 votes
/**
 * Sets the value for the key given, returns true if successful, false otherwise.
 */
/* package */ static boolean setItemImpl(SQLiteDatabase db, String key, String value) {
  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_COLUMN, key);
  contentValues.put(VALUE_COLUMN, value);

  long inserted = db.insertWithOnConflict(
      TABLE_CATALYST,
      null,
      contentValues,
      SQLiteDatabase.CONFLICT_REPLACE);

  return (-1 != inserted);
}
 
Example 6
Project: androidtv-sample   File: VideoProvider.java   View Source Code Vote up 5 votes
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
    switch (sUriMatcher.match(uri)) {
        case VIDEO: {
            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            int returnCount = 0;

            db.beginTransaction();
            try {
                for (ContentValues value : values) {
                    long _id = db.insertWithOnConflict(VideoContract.VideoEntry.TABLE_NAME,
                            null, value, SQLiteDatabase.CONFLICT_REPLACE);
                    if (_id != -1) {
                        returnCount++;
                    }
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }

            mContentResolver.notifyChange(uri, null);
            return returnCount;
        }
        default: {
            return super.bulkInsert(uri, values);
        }
    }
}
 
Example 7
Project: minu-poska-android   File: TimetableDatabaseHelper.java   View Source Code Vote up 5 votes
public boolean insertTimetableTeacher(int id, String name, String shortname, int colour, String timetableID) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_TEACHER_ID, id);
    contentValues.put(COL_NAME, name);
    contentValues.put(COL_SHORT_NAME, shortname);
    contentValues.put(COL_COLOUR, colour);
    contentValues.put(COL_TIMETABLE_ID, timetableID);
    long result = db.insertWithOnConflict(TABLE_TEACHERS, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    return result != -1;
}
 
Example 8
Project: MusicX-music-player   File: CommonDatabase.java   View Source Code Vote up 5 votes
private void addArtistMetaData(SQLiteDatabase sqLiteDatabase, Artist artist) {
    ContentValues values = new ContentValues();
    values.put(ArtistId, artist.getId());
    values.put(ArtistTitle, artist.getName());
    values.put(ArtistAlbumCount, artist.getAlbumCount());
    values.put(ArtistTrackCount, artist.getTrackCount());
    sqLiteDatabase.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
 
Example 9
Project: TripBuyer   File: LCIMLocalStorage.java   View Source Code Vote up 5 votes
/**
 * 插入数据,此为同步方法
 */
private void insertSync(List<String> idList, List<String> valueList) {
  SQLiteDatabase db = getWritableDatabase();
  db.beginTransaction();
  for (int i = 0; i < valueList.size(); i++) {
    ContentValues values = new ContentValues();
    values.put(TABLE_KEY_ID, idList.get(i));
    values.put(TABLE_KEY_CONTENT, valueList.get(0));
    db.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE);
  }
  db.setTransactionSuccessful();
  db.endTransaction();
}
 
Example 10
Project: MusicX-music-player   File: CommonDatabase.java   View Source Code Vote up 5 votes
private void addSongMetaData(SQLiteDatabase sqLiteDatabase, Song song) {
    ContentValues values = new ContentValues();
    values.put(SongId, song.getId());
    values.put(SongTitle, song.getTitle());
    values.put(SongAlbum, song.getAlbum());
    values.put(SongArtist, song.getArtist());
    values.put(SongNumber, song.getTrackNumber());
    values.put(SongPath, song.getmSongPath());
    values.put(SongAlbumId, song.getAlbumId());
    sqLiteDatabase.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
 
Example 11
Project: android-architecture-components   File: SqliteDatabaseTestHelper.java   View Source Code Vote up 5 votes
public static void insertUser(int userid, String userName, SqliteTestDbOpenHelper helper) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("userid", userid);
    values.put("username", userName);

    db.insertWithOnConflict("users", null, values,
            SQLiteDatabase.CONFLICT_REPLACE);

    db.close();
}
 
Example 12
Project: android-architecture-components   File: LocalUserDataSource.java   View Source Code Vote up 5 votes
@Override
public void insertOrUpdateUser(User user) {
    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME_ENTRY_ID, user.getId());
    values.put(COLUMN_NAME_USERNAME, user.getUserName());

    db.insertWithOnConflict(TABLE_NAME, null, values,
            SQLiteDatabase.CONFLICT_REPLACE);

    db.close();
}
 
Example 13
Project: chilly   File: VideoProvider.java   View Source Code Vote up 5 votes
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
    switch (sUriMatcher.match(uri)) {
        case VIDEO: {
            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            int returnCount = 0;

            db.beginTransaction();
            try {
                for (ContentValues value : values) {
                    long _id = db.insertWithOnConflict(VideoContract.VideoEntry.TABLE_NAME,
                            null, value, SQLiteDatabase.CONFLICT_REPLACE);
                    if (_id != -1) {
                        returnCount++;
                    }
                }
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }

            mContentResolver.notifyChange(uri, null);
            return returnCount;
        }
        default: {
            return super.bulkInsert(uri, values);
        }
    }
}
 
Example 14
Project: minu-poska-android   File: TimetableDatabaseHelper.java   View Source Code Vote up 5 votes
public boolean insertTimetableMeta(String timetable_id, long modified) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_TIMETABLE_ID, timetable_id);
    contentValues.put(COL_MODIFIED_MILLIS, modified);
    long result = db.insertWithOnConflict(TABLE_META, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    return result != -1;
}
 
Example 15
Project: minu-poska-android   File: TimetableDatabaseHelper.java   View Source Code Vote up 5 votes
public boolean insertTimetableForm(int id, String name, String shortname, int teacherid, String timetableID) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_FORM_ID, id);
    contentValues.put(COL_NAME, name);
    contentValues.put(COL_SHORT_NAME, shortname);
    contentValues.put(COL_TEACHER_ID, teacherid);
    contentValues.put(COL_TIMETABLE_ID, timetableID);
    long result = db.insertWithOnConflict(TABLE_FORMS, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    return result != -1;
}
 
Example 16
Project: Melophile   File: MusicProvider.java   View Source Code Vote up 5 votes
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    if(values==null) {
        throw new IllegalArgumentException("Values are null");
    }
    final SQLiteDatabase db=database.getWritableDatabase();
    final MusicMatchEnum matchEnum=matcher.match(uri);
    if(matchEnum.table!=null){
        db.insertWithOnConflict(matchEnum.table,null,values,SQLiteDatabase.CONFLICT_REPLACE);
        notifyChange(uri);
    }
    return null;
}
 
Example 17
Project: minu-poska-android   File: TimetableDatabaseHelper.java   View Source Code Vote up 5 votes
public boolean insertTimetableSchedule(int dayid, int periodid, int classroomid, int teacherid, int subjectid, String formids, String timetableID) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_DAY_ID, dayid);
    contentValues.put(COL_PERIOD_ID, periodid);
    contentValues.put(COL_CLASSROOM_ID, classroomid);
    contentValues.put(COL_TEACHER_ID, teacherid);
    contentValues.put(COL_SUBJECT_ID, subjectid);
    contentValues.put(COL_FORM_ID, formids);
    contentValues.put(COL_TIMETABLE_ID, timetableID);
    long result = db.insertWithOnConflict(TABLE_SCHEDULES, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    return result != -1;
}
 
Example 18
Project: simple-share-android   File: RecentsProvider.java   View Source Code Vote up 4 votes
@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:
            throw new UnsupportedOperationException("Unsupported Uri " + uri);
    }
}
 
Example 19
Project: XPrivacy   File: PrivacyService.java   View Source Code Vote up 4 votes
@Override
public void clear() throws RemoteException {
	try {
		enforcePermission(0);
		SQLiteDatabase db = getDb();
		SQLiteDatabase dbUsage = getDbUsage();
		if (db == null || dbUsage == null)
			return;

		mLock.writeLock().lock();
		try {
			db.beginTransaction();
			try {
				db.execSQL("DELETE FROM " + cTableRestriction);
				db.execSQL("DELETE FROM " + cTableSetting);
				Util.log(null, Log.WARN, "Database cleared");

				// Reset migrated
				ContentValues values = new ContentValues();
				values.put("uid", 0);
				values.put("type", "");
				values.put("name", PrivacyManager.cSettingMigrated);
				values.put("value", Boolean.toString(true));
				db.insertWithOnConflict(cTableSetting, null, values, SQLiteDatabase.CONFLICT_REPLACE);

				db.setTransactionSuccessful();
			} finally {
				db.endTransaction();
			}
		} finally {
			mLock.writeLock().unlock();
		}

		// Clear caches
		synchronized (mRestrictionCache) {
			mRestrictionCache.clear();
		}
		synchronized (mSettingCache) {
			mSettingCache.clear();
		}
		synchronized (mAskedOnceCache) {
			mAskedOnceCache.clear();
		}
		Util.log(null, Log.WARN, "Caches cleared");

		mLockUsage.writeLock().lock();
		try {
			dbUsage.beginTransaction();
			try {
				dbUsage.execSQL("DELETE FROM " + cTableUsage);
				Util.log(null, Log.WARN, "Usage database cleared");

				dbUsage.setTransactionSuccessful();
			} finally {
				dbUsage.endTransaction();
			}
		} finally {
			mLockUsage.writeLock().unlock();
		}

	} catch (Throwable ex) {
		Util.bug(null, ex);
		throw new RemoteException(ex.toString());
	}
}
 
Example 20
Project: FireFiles   File: RecentsProvider.java   View Source Code Vote up 4 votes
@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:
            throw new UnsupportedOperationException("Unsupported Uri " + uri);
    }
}