/** * Efficiently insert a collection of entities using {@link InsertHelper}. * * @param many Collection of objects * @return count of inserted objects or -1 immediately if any errors */ public long insertMany(Iterable<T> many) { long numInserted = 0; InsertHelper insertHelper = new DatabaseUtils.InsertHelper(getWritableDb(), th.getTableName()); getWritableDb().beginTransaction(); try { for (T obj : many) { ContentValues cv = th.getEditableValues(obj); if (th.getId(obj) == 0) { // the default, remove from ContentValues to allow autoincrement cv.remove(th.getIdCol().toString()); } long id = insertHelper.insert(cv); if (id == -1) return -1; numInserted++; } getWritableDb().setTransactionSuccessful(); } finally { getWritableDb().endTransaction(); } return numInserted; }
@Override public int bulkInsert(Uri uri, ContentValues[] valuesAr) { final String table; switch (matcher.match(uri)) { case MATCH_TQUESTIONS_URI_CONTENT: { table = PollsData.TQuestions.TABLE_NAME; break; } case MATCH_TPOLLGROUPS_URI_CONTENT: { table = PollsData.TPollGroups.TABLE_NAME; break; } case MATCH_TPOLLS_URI_CONTENT: { table = PollsData.TPolls.TABLE_NAME; break; } case MATCH_TVARIANTS_URI_CONTENT: { table = PollsData.TVariants.TABLE_NAME; break; } case MATCH_TPOINT_HISTORY_URI_CONTENT: { table = PollsData.TPointHistory.TABLE_NAME; break; } default: throw new IllegalArgumentException("Unsupported uri " + uri); } SQLiteDatabase sql = dbHelper.getWritableDatabase(); sql.beginTransaction(); int count = 0; try { InsertHelper ih = new InsertHelper(sql, table); for (ContentValues values : valuesAr) { ih.replace(values); count++; } ih.close(); sql.setTransactionSuccessful(); } finally { sql.endTransaction(); } if (!ignoreNotify(uri)) { notifyUri(contentResolver, uri); } return count; }
@Override public int bulkInsert(Uri uri, ContentValues[] valuesAr) { final String table; switch(matcher.match(uri)){ case MATCH_CARS_URI_CONTENT:{ table = Cars.TABLE_NAME; break; } case MATCH_BLACKLIST_URI_CONTENT:{ table = BlackList.TABLE_NAME; break; } case MATCH_TIMES_URI_CONTENT:{ table = Times.TABLE_NAME; break; } case MATCH_SUBSCRIPTIONS_URI_CONTENT:{ table = Subscriptions.TABLE_NAME; break; } case MATCH_ROADS_URI_CONTENT:{ table = Roads.TABLE_NAME; break; } case MATCH_ROADGROUPS_URI_CONTENT:{ table = RoadGroups.TABLE_NAME; break; } case MATCH_FLATS_URI_CONTENT:{ table = Flats.TABLE_NAME; break; } default: throw new IllegalArgumentException("Unsupported uri " + uri); } SQLiteDatabase sql = dbHelper.getWritableDatabase(); sql.beginTransaction(); int count = 0; try { InsertHelper ih = new InsertHelper(sql, table); for (ContentValues values : valuesAr) { ih.replace(values); count++; } ih.close(); sql.setTransactionSuccessful(); } finally { sql.endTransaction(); } if (!ignoreNotify(uri)) { notifyUri(contentResolver, uri); } return count; }
public boolean insertBulkItems(ArrayList<FeedObject> feedList) { long start = System.currentTimeMillis(); int time = (int)(start / 1000); if(feedList==null || feedList.size()<1) return false; Logs.d(TAG, "# Insert bulk : type="+feedList.get(0).mType+", item count = "+feedList.size()); InsertHelper iHelp = new InsertHelper(mDb, TABLE_NAME_FEED_ITEM); int ktype = iHelp.getColumnIndex(KEY_FEED_TYPE); int kstatus = iHelp.getColumnIndex(KEY_FEED_STATUS); int kid = iHelp.getColumnIndex(KEY_FEED_IDSTRING); int kname = iHelp.getColumnIndex(KEY_FEED_NAME); int klink = iHelp.getColumnIndex(KEY_FEED_LINK); int kkeyword = iHelp.getColumnIndex(KEY_FEED_KEYWORD); int kcontent = iHelp.getColumnIndex(KEY_FEED_CONTENT); int kthumbnail = iHelp.getColumnIndex(KEY_FEED_THUMBNAILURL); int kdate = iHelp.getColumnIndex(KEY_FEED_DATE); int krank = iHelp.getColumnIndex(KEY_FEED_RANK); int kclick = iHelp.getColumnIndex(KEY_FEED_CLICK); int kranktype = iHelp.getColumnIndex(KEY_FEED_ARG0); int kversion = iHelp.getColumnIndex(KEY_FEED_ARG1); int kfullimage = iHelp.getColumnIndex(KEY_FEED_ARG2); synchronized (mDb) { if(mDb == null) return false; try { mDb.beginTransaction(); // First one is recent one. So insert oldest first. for(int i = feedList.size()-1 ; -1<i ; i--) { FeedObject feed = feedList.get(i); // need to tell the helper you are inserting (rather than replacing) iHelp.prepareForInsert(); // do the equivalent of ContentValues.put("field","value") here iHelp.bind(ktype, feed.mType); iHelp.bind(kstatus, feed.mDownloadStatus); iHelp.bind(kid, feed.mId); if(feed.mName != null) iHelp.bind(kname, feed.mName); if(feed.mLink != null) iHelp.bind(klink, feed.mLink); if(feed.mKeyword != null) iHelp.bind(kkeyword, feed.mKeyword); if(feed.mContent != null) iHelp.bind(kcontent, feed.mContent); if(feed.mThumbnailUrl != null) iHelp.bind(kthumbnail, feed.mThumbnailUrl); if(feed.mDate != null) iHelp.bind(kdate, feed.mDate); else iHelp.bind(kdate, time); iHelp.bind(krank, feed.mRankUpAndDown); iHelp.bind(kclick, feed.mCommentCount); iHelp.bind(kranktype, feed.mRankType); iHelp.bind(kversion, feed.mVersion); if(feed.mFullSizeImageURL != null) iHelp.bind(kfullimage, feed.mFullSizeImageURL); //the db.insert() equilvalent iHelp.execute(); } mDb.setTransactionSuccessful(); } catch(Exception e) { } finally { mDb.endTransaction(); } } return true; }
private int bulkInsertUsers(final ContentValues[] values) { int numInserted = 0; final SQLiteDatabase db = this.dbHelper.getWritableDatabase(); final InsertHelper ih = new InsertHelper(db, UserInfo.TABLE_NAME); final int id = ih.getColumnIndex(BasicColumns.ID); final int ownerId = ih.getColumnIndex(BasicColumns.OWNER_ID); final int screenName = ih.getColumnIndex(UserInfo.SCREEN_NAME); final int location = ih.getColumnIndex(UserInfo.LOCATION); final int gender = ih.getColumnIndex(UserInfo.GENDER); final int birthday = ih.getColumnIndex(UserInfo.BIRTHDAY); final int description = ih.getColumnIndex(UserInfo.DESCRIPTION); final int profileImageUrl = ih .getColumnIndex(UserInfo.PROFILE_IMAGE_URL); final int url = ih.getColumnIndex(UserInfo.URL); final int protect = ih.getColumnIndex(UserInfo.PROTECTED); final int followersCount = ih.getColumnIndex(UserInfo.FOLLOWERS_COUNT); final int friendsCount = ih.getColumnIndex(UserInfo.FRIENDS_COUNT); final int favoritesCount = ih.getColumnIndex(UserInfo.FAVORITES_COUNT); final int statusesCount = ih.getColumnIndex(UserInfo.STATUSES_COUNT); final int following = ih.getColumnIndex(UserInfo.FOLLOWING); final int createdAt = ih.getColumnIndex(BasicColumns.CREATED_AT); final int type = ih.getColumnIndex(BasicColumns.TYPE); try { db.beginTransaction(); for (final ContentValues value : values) { ih.prepareForInsert(); ih.bind(id, value.getAsString(BasicColumns.ID)); ih.bind(ownerId, value.getAsString(BasicColumns.OWNER_ID)); ih.bind(screenName, value.getAsString(UserInfo.SCREEN_NAME)); ih.bind(location, value.getAsString(UserInfo.LOCATION)); ih.bind(gender, value.getAsString(UserInfo.GENDER)); ih.bind(birthday, value.getAsString(UserInfo.BIRTHDAY)); ih.bind(description, value.getAsString(UserInfo.DESCRIPTION)); ih.bind(profileImageUrl, value.getAsString(UserInfo.PROFILE_IMAGE_URL)); ih.bind(url, value.getAsString(UserInfo.URL)); ih.bind(protect, value.getAsBoolean(UserInfo.PROTECTED)); ih.bind(followersCount, value.getAsInteger(UserInfo.FOLLOWERS_COUNT)); ih.bind(friendsCount, value.getAsInteger(UserInfo.FRIENDS_COUNT)); ih.bind(favoritesCount, value.getAsInteger(UserInfo.FAVORITES_COUNT)); ih.bind(statusesCount, value.getAsInteger(UserInfo.STATUSES_COUNT)); ih.bind(following, value.getAsBoolean(UserInfo.FOLLOWING)); ih.bind(createdAt, value.getAsLong(BasicColumns.CREATED_AT)); ih.bind(type, value.getAsInteger(BasicColumns.TYPE)); final long result = ih.execute(); if (result > -1) { numInserted++; } } if (AppContext.DEBUG) { log("bulkInsertUsers insert rows=" + numInserted); } db.setTransactionSuccessful(); } catch (final Exception e) { if (AppContext.DEBUG) { e.printStackTrace(); } } finally { ih.close(); db.endTransaction(); } return numInserted; }
public void insertBulkCallList(ArrayList<SMSMessage> smsList){ SQLiteDatabase db = this.getWritableDatabase(); try{ // Create a single InsertHelper to handle this set of insertions. InsertHelper ih = new InsertHelper(db, SMSListTable.TABLE_NAME); // Get the numeric indexes for each of the columns that we're updating final int smsIdColumn = ih.getColumnIndex(SMSListTable.smsId); final int phoneColumn = ih.getColumnIndex(SMSListTable.phoneNumber); final int threadIDColumn = ih.getColumnIndex(SMSListTable.threadID); final int typeColumn = ih.getColumnIndex(SMSListTable.type); final int dateColumn = ih.getColumnIndex(SMSListTable.date); final int smsPartsColumn = ih.getColumnIndex(SMSListTable.smsParts); try { for (SMSMessage smsMessage : smsList) { if(smsMessage!=null){ // ... Create the data for this row (not shown) ... // Get the InsertHelper ready to insert a single row ih.prepareForInsert(); // Add the data for each column ih.bind(smsIdColumn, smsMessage.getId()); ih.bind(phoneColumn, smsMessage.getNumber()); ih.bind(threadIDColumn, smsMessage.getThreadID()); ih.bind(typeColumn, smsMessage.getType()); ih.bind(dateColumn, smsMessage.getDate()); ih.bind(smsPartsColumn, smsMessage.getSmsParts()); // Insert the row into the database. ih.execute(); } } }catch(Exception e){ e.printStackTrace(); }finally { if(ih!=null ) ih.close(); // See comment below from Stefan Anca } }finally{ closeDatabase(db); } }
/** * Convert the String values for each column into their appropriate SQL * types and bind to the insert statement using {@link InsertHelper}. * * @param insHelper * @param rowValues */ public abstract void bindRowValues(InsertHelper insHelper, String[] rowValues);