SQLiteDatabase sqlDB = mDB.getWritableDatabase(); sqlDB.beginTransaction(); try { for (ContentValues cv : values) { long newID = sqlDB.insertOrThrow(table, null, cv); if (newID <= 0) { throw new SQLException("Failed to insert row into " + uri); } } sqlDB.setTransactionSuccessful(); getContext().getContentResolver().notifyChange(uri, null); numInserted = values.length; } finally { sqlDB.endTransaction(); }
public int bulkInsert(Uri uri, ContentValues[] values){ int numInserted = 0; String table; int uriType = sURIMatcher.match(uri); switch (uriType) { case PEOPLE: table = TABLE_PEOPLE; break; } SQLiteDatabase sqlDB = database.getWritableDatabase(); sqlDB.beginTransaction(); try { for (ContentValues cv : values) { long newID = sqlDB.insertOrThrow(table, null, cv); if (newID <= 0) { throw new SQLException("Failed to insert row into " + uri); } } sqlDB.setTransactionSuccessful(); getContext().getContentResolver().notifyChange(uri, null); numInserted = values.length; } finally { sqlDB.endTransaction(); } return numInserted; }
@Test(expected = android.database.SQLException.class) public void testInsertOrThrowWithSQLException() { ContentValues values = new ContentValues(); values.put("id", 1); database.insertOrThrow("table_name", null, values); database.insertOrThrow("table_name", null, values); }
ContentValues values) { try { return db.insertOrThrow(table, null, values); } catch (SQLException e) { StringBuilder sql = new StringBuilder();
structureState.applyValues(cv); long partId = db.insertOrThrow("message_parts", null, cv); return structureState.nextChild(partId);
@Test public void testInsertOrThrow() { String stringColumnValue = "column_value"; byte[] byteColumnValue = new byte[]{1, 2, 3}; ContentValues values = new ContentValues(); values.put("first_column", stringColumnValue); values.put("second_column", byteColumnValue); database.insertOrThrow("table_name", null, values); Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null); assertThat(cursor.moveToFirst()).isTrue(); byte[] byteValueFromDatabase = cursor.getBlob(0); String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); }
private long updateOrInsertMessagePart(SQLiteDatabase db, ContentValues cv, Part part, long existingMessagePartId) throws IOException, MessagingException { byte[] headerBytes = getHeaderBytes(part); cv.put("mime_type", part.getMimeType()); cv.put("header", headerBytes); cv.put("type", MessagePartType.UNKNOWN); File file = null; Body body = part.getBody(); if (body instanceof Multipart) { multipartToContentValues(cv, (Multipart) body); } else if (body == null) { missingPartToContentValues(cv, part); } else if (body instanceof Message) { messageMarkerToContentValues(cv); } else { file = leafPartToContentValues(cv, part, body); } long messagePartId; if (existingMessagePartId != INVALID_MESSAGE_PART_ID) { messagePartId = existingMessagePartId; db.update("message_parts", cv, "id = ?", new String[] { Long.toString(messagePartId) }); } else { messagePartId = db.insertOrThrow("message_parts", null, cv); } if (file != null) { moveTemporaryFile(file, Long.toString(messagePartId)); } return messagePartId; }
structureState.applyValues(cv); long partId = db.insertOrThrow("message_parts", null, cv); structureState = structureState.nextChild(partId);
structureState.applyValues(cv); long rootMessagePartId = db.insertOrThrow("message_parts", null, cv); structureState = structureState.nextMultipartChild(rootMessagePartId);
structureState.applyValues(cv); long multipartAlternativePartId = db.insertOrThrow("message_parts", null, cv); structureState = structureState.nextMultipartChild(multipartAlternativePartId);
structureState.applyValues(cv); long rootMessagePartId = db.insertOrThrow("message_parts", null, cv); structureState = structureState.nextMultipartChild(rootMessagePartId);
@Override public Long then(Task<Void> task) { return db.insertOrThrow(table, null, values); } }, dbExecutor);
/** * Inserts a record to the table. * * @param uri The Uri of a table. * @param values The values of a record. * @return The Uri of the inserted record. */ public Uri insert(Uri uri, ContentValues values) { final int uriType = uriMatcher.match(uri); long id; ensureDatabaseOpen(); switch (uriType) { case TRANSFERS: id = database.insertOrThrow(TransferTable.TABLE_TRANSFER, null, values); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } return Uri.parse(BASE_PATH + "/" + id); }
try { database.beginTransaction(); mainUploadId = (int) database.insertOrThrow(TransferTable.TABLE_TRANSFER, null, valuesArray[0]); for (int i = 1; i < valuesArray.length; i++) { valuesArray[i].put(TransferTable.COLUMN_MAIN_UPLOAD_ID, mainUploadId); database.insertOrThrow(TransferTable.TABLE_TRANSFER, null, valuesArray[i]);
/** * Inserts a record to the table. * * @param uri The Uri of a table. * @param values The values of a record. * @return The Uri of the inserted record. */ public Uri insert(final Uri uri, final ContentValues values) { final int uriType = uriMatcher.match(uri); final SQLiteDatabase db = databaseHelper.getWritableDatabase(); long id = 0; switch (uriType) { case EVENTS: final long size = getTotalSize(); id = db.insertOrThrow(TABLE_EVENT, null, values); totalSize = size + values.getAsLong(COLUMN_SIZE); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } return Uri.parse(BASE_PATH + "/" + id); }
values.put(MTIMESTAMP, System.currentTimeMillis()); try { db.insertOrThrow(ARRIVED_MESSAGE_TABLE_NAME, null, values); } catch (SQLException e) { traceHandler.traceException(TAG, "onUpgrade", e);
try { SQLiteDatabase db = SQLiteHelper.getDatabase(); db.insertOrThrow(Table, nullColumnHack, values); } catch (SQLiteConstraintException e) { Log.d(TAG, "failure to insert word,", e); return false; }
public long newPointPing(long point_id, long ping_id) throws Exception { ContentValues init = new ContentValues(); init.put(KEY_POINTID, point_id); init.put(KEY_PID, ping_id); return mDb.insertOrThrow(POINTPINGS_TABLE, null, init); }
/** * Attempts to create a new ping/tag pair, throwing an exception if an * identical one already exists */ public long newTagPing(long pingid, long tagid) throws Exception { ContentValues init = new ContentValues(); init.put(KEY_PID, pingid); init.put(KEY_TID, tagid); return mDb.insertOrThrow(TAG_PING_TABLE, null, init); }
public void insertDomainEntry(Date exp_date, String name, long event_id){ SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put("exp_date", exp_date.getTime()/1000); values.put("event_id", event_id); values.put("domainname", name); db.insertOrThrow("domains_events", null, values); }