下面列出了android.database.sqlite.SQLiteDatabase#replaceOrThrow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void putDownload(Download download) throws DatabaseIOException {
ensureInitialized();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, download.request.id);
values.put(COLUMN_TYPE, download.request.type);
values.put(COLUMN_URI, download.request.uri.toString());
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(download.request.streamKeys));
values.put(COLUMN_CUSTOM_CACHE_KEY, download.request.customCacheKey);
values.put(COLUMN_DATA, download.request.data);
values.put(COLUMN_STATE, download.state);
values.put(COLUMN_START_TIME_MS, download.startTimeMs);
values.put(COLUMN_UPDATE_TIME_MS, download.updateTimeMs);
values.put(COLUMN_CONTENT_LENGTH, download.contentLength);
values.put(COLUMN_STOP_REASON, download.stopReason);
values.put(COLUMN_FAILURE_REASON, download.failureReason);
values.put(COLUMN_PERCENT_DOWNLOADED, download.getPercentDownloaded());
values.put(COLUMN_BYTES_DOWNLOADED, download.getBytesDownloaded());
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
} catch (SQLiteException e) {
throw new DatabaseIOException(e);
}
}
@Override
public void putDownload(Download download) throws DatabaseIOException {
ensureInitialized();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, download.request.id);
values.put(COLUMN_TYPE, download.request.type);
values.put(COLUMN_URI, download.request.uri.toString());
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(download.request.streamKeys));
values.put(COLUMN_CUSTOM_CACHE_KEY, download.request.customCacheKey);
values.put(COLUMN_DATA, download.request.data);
values.put(COLUMN_STATE, download.state);
values.put(COLUMN_START_TIME_MS, download.startTimeMs);
values.put(COLUMN_UPDATE_TIME_MS, download.updateTimeMs);
values.put(COLUMN_CONTENT_LENGTH, download.contentLength);
values.put(COLUMN_STOP_REASON, download.stopReason);
values.put(COLUMN_FAILURE_REASON, download.failureReason);
values.put(COLUMN_PERCENT_DOWNLOADED, download.getPercentDownloaded());
values.put(COLUMN_BYTES_DOWNLOADED, download.getBytesDownloaded());
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
} catch (SQLiteException e) {
throw new DatabaseIOException(e);
}
}
@Override
public void putDownload(Download download) throws DatabaseIOException {
ensureInitialized();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, download.request.id);
values.put(COLUMN_TYPE, download.request.type);
values.put(COLUMN_URI, download.request.uri.toString());
values.put(COLUMN_STREAM_KEYS, encodeStreamKeys(download.request.streamKeys));
values.put(COLUMN_CUSTOM_CACHE_KEY, download.request.customCacheKey);
values.put(COLUMN_DATA, download.request.data);
values.put(COLUMN_STATE, download.state);
values.put(COLUMN_START_TIME_MS, download.startTimeMs);
values.put(COLUMN_UPDATE_TIME_MS, download.updateTimeMs);
values.put(COLUMN_CONTENT_LENGTH, download.contentLength);
values.put(COLUMN_STOP_REASON, download.stopReason);
values.put(COLUMN_FAILURE_REASON, download.failureReason);
values.put(COLUMN_PERCENT_DOWNLOADED, download.getPercentDownloaded());
values.put(COLUMN_BYTES_DOWNLOADED, download.getBytesDownloaded());
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
} catch (SQLiteException e) {
throw new DatabaseIOException(e);
}
}
/**
* Sets the version of a specified instance of a specified feature.
*
* @param writableDatabase The database to update.
* @param feature The feature.
* @param instanceUid The unique identifier of the instance of the feature.
* @param version The version.
* @throws DatabaseIOException If an error occurs executing the SQL.
*/
public static void setVersion(
SQLiteDatabase writableDatabase, @Feature int feature, String instanceUid, int version)
throws DatabaseIOException {
try {
writableDatabase.execSQL(SQL_CREATE_TABLE_IF_NOT_EXISTS);
ContentValues values = new ContentValues();
values.put(COLUMN_FEATURE, feature);
values.put(COLUMN_INSTANCE_UID, instanceUid);
values.put(COLUMN_VERSION, version);
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
private void addOrUpdateRow(SQLiteDatabase writableDatabase, CachedContent cachedContent)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writeContentMetadata(cachedContent.getMetadata(), new DataOutputStream(outputStream));
byte[] data = outputStream.toByteArray();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, cachedContent.id);
values.put(COLUMN_KEY, cachedContent.key);
values.put(COLUMN_METADATA, data);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
}
/**
* Sets metadata for a given file.
*
* <p>This method may be slow and shouldn't normally be called on the main thread.
*
* @param name The name of the file.
* @param length The file length.
* @param lastTouchTimestamp The file last touch timestamp.
* @throws DatabaseIOException If an error occurs setting the metadata.
*/
@WorkerThread
public void set(String name, long length, long lastTouchTimestamp) throws DatabaseIOException {
Assertions.checkNotNull(tableName);
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LENGTH, length);
values.put(COLUMN_LAST_TOUCH_TIMESTAMP, lastTouchTimestamp);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
public void createOrUpdateSingleNoteWidgetData(@NonNull SingleNoteWidgetData data) throws SQLException {
validateAccountId(data.getAccountId());
final SQLiteDatabase db = getWritableDatabase();
final ContentValues values = new ContentValues(4);
values.put(key_id, data.getAppWidgetId());
values.put(key_account_id, data.getAccountId());
values.put(key_note_id, data.getNoteId());
values.put(key_theme_mode, data.getThemeMode());
db.replaceOrThrow(table_widget_single_notes, null, values);
}
public void createOrUpdateNoteListWidgetData(@NonNull NoteListsWidgetData data) throws SQLException {
validateAccountId(data.getAccountId());
final SQLiteDatabase db = getWritableDatabase();
final ContentValues values = new ContentValues(5);
if (data.getMode() != MODE_DISPLAY_CATEGORY && data.getCategoryId() != null) {
throw new UnsupportedOperationException("Cannot create a widget with a categoryId when mode is not " + MODE_DISPLAY_CATEGORY);
}
values.put(key_id, data.getAppWidgetId());
values.put(key_account_id, data.getAccountId());
values.put(key_category_id, data.getCategoryId());
values.put(key_theme_mode, data.getThemeMode());
values.put(key_mode, data.getMode());
db.replaceOrThrow(table_widget_note_list, null, values);
}
/**
* Sets the version of a specified instance of a specified feature.
*
* @param writableDatabase The database to update.
* @param feature The feature.
* @param instanceUid The unique identifier of the instance of the feature.
* @param version The version.
* @throws DatabaseIOException If an error occurs executing the SQL.
*/
public static void setVersion(
SQLiteDatabase writableDatabase, @Feature int feature, String instanceUid, int version)
throws DatabaseIOException {
try {
writableDatabase.execSQL(SQL_CREATE_TABLE_IF_NOT_EXISTS);
ContentValues values = new ContentValues();
values.put(COLUMN_FEATURE, feature);
values.put(COLUMN_INSTANCE_UID, instanceUid);
values.put(COLUMN_VERSION, version);
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
private void addOrUpdateRow(SQLiteDatabase writableDatabase, CachedContent cachedContent)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writeContentMetadata(cachedContent.getMetadata(), new DataOutputStream(outputStream));
byte[] data = outputStream.toByteArray();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, cachedContent.id);
values.put(COLUMN_KEY, cachedContent.key);
values.put(COLUMN_METADATA, data);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
}
/**
* Sets metadata for a given file.
*
* @param name The name of the file.
* @param length The file length.
* @param lastTouchTimestamp The file last touch timestamp.
* @throws DatabaseIOException If an error occurs setting the metadata.
*/
public void set(String name, long length, long lastTouchTimestamp) throws DatabaseIOException {
Assertions.checkNotNull(tableName);
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LENGTH, length);
values.put(COLUMN_LAST_TOUCH_TIMESTAMP, lastTouchTimestamp);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
/**
* Sets the version of a specified instance of a specified feature.
*
* @param writableDatabase The database to update.
* @param feature The feature.
* @param instanceUid The unique identifier of the instance of the feature.
* @param version The version.
* @throws DatabaseIOException If an error occurs executing the SQL.
*/
public static void setVersion(
SQLiteDatabase writableDatabase, @Feature int feature, String instanceUid, int version)
throws DatabaseIOException {
try {
writableDatabase.execSQL(SQL_CREATE_TABLE_IF_NOT_EXISTS);
ContentValues values = new ContentValues();
values.put(COLUMN_FEATURE, feature);
values.put(COLUMN_INSTANCE_UID, instanceUid);
values.put(COLUMN_VERSION, version);
writableDatabase.replaceOrThrow(TABLE_NAME, /* nullColumnHack= */ null, values);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
private void addOrUpdateRow(SQLiteDatabase writableDatabase, CachedContent cachedContent)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writeContentMetadata(cachedContent.getMetadata(), new DataOutputStream(outputStream));
byte[] data = outputStream.toByteArray();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, cachedContent.id);
values.put(COLUMN_KEY, cachedContent.key);
values.put(COLUMN_METADATA, data);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
}
/**
* Sets metadata for a given file.
*
* @param name The name of the file.
* @param length The file length.
* @param lastTouchTimestamp The file last touch timestamp.
* @throws DatabaseIOException If an error occurs setting the metadata.
*/
public void set(String name, long length, long lastTouchTimestamp) throws DatabaseIOException {
Assertions.checkNotNull(tableName);
try {
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LENGTH, length);
values.put(COLUMN_LAST_TOUCH_TIMESTAMP, lastTouchTimestamp);
writableDatabase.replaceOrThrow(tableName, /* nullColumnHack= */ null, values);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}