Skip to content Skip to sidebar Skip to footer

Multiple Rows Insert With Contentprovider

I need to make insert of few rows in one transaction. Can I do it with ContentProvider?

Solution 1:

I have implemented this in my app and here's the gist of the code that I use.

In my content provider, I have overridden the applyBatch() method and it's a very simple method to override:

/**
 * Performs the work provided in a single transaction
 */
@Override
public ContentProviderResult[] applyBatch(
        ArrayList<ContentProviderOperation> operations) {
    ContentProviderResult[] result = new ContentProviderResult[operations
            .size()];
    int i = 0;
    // Opens the database object in "write" mode.
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    // Begin a transaction
    db.beginTransaction();
    try {
        for (ContentProviderOperation operation : operations) {
            // Chain the result for back references
            result[i++] = operation.apply(this, result, i);
        }

        db.setTransactionSuccessful();
    } catch (OperationApplicationException e) {
        Log.d(TAG, "batch failed: " + e.getLocalizedMessage());
    } finally {
        db.endTransaction();
    }

    return result;
}

The result is given to the next operation because you want to support back references. When I actually want to change stuff in the database in this single transaction I loop over my content and do stuff like this:

operations.add(ContentProviderOperation
                    .newInsert(
                            Uri.withAppendedPath(
                                    NotePad.Notes.CONTENT_ID_URI_BASE,
                                    Long.toString(task.dbId)))
                    .withValues(task.toNotesContentValues(0, listDbId))
                    .build());
// Now the other table, use back reference to the id the note// received
noteIdIndex = operations.size() - 1;

operations.add(ContentProviderOperation
                    .newInsert(NotePad.GTasks.CONTENT_URI)
                    .withValues(task.toGTasksContentValues(accountName))
                    .withValueBackReferences(
                            task.toGTasksBackRefContentValues(noteIdIndex))
                    .build());

You just need to remember to finish by calling:

provider.applyBatch(operations);

This will perform your stuff in a single transaction and supports backreferences if you need the id from an earlier insert without issue.

Solution 2:

On the client side, ContentResolver supports a bulkInsert() method. Those will not necessarily be processed in a single transaction by the ContentProvider, simply because there may not be any transactions performed by the ContentProvider.

Solution 3:

Here an example for bulkInsert:

/**
 * Perform bulkInsert with use of transaction
 */@OverridepublicintbulkInsert(Uri uri, ContentValues[] values) {
    inturiType=0;
    intinsertCount=0;
    try {

        uriType = sURIMatcher.match(uri);
        SQLiteDatabasesqlDB= dbHelper.getWritableDatabase();

        switch (uriType) {
        case MEASUREMENTS:
            try {
                sqlDB.beginTransaction();
                for (ContentValues value : values) {
                    longid= sqlDB.insert(Tab_Measurements.TABLE_NAME, null, value);
                    if (id > 0)
                        insertCount++;
                }
                sqlDB.setTransactionSuccessful();
            } catch (Exception e) {
                // Your error handling
            } finally {
                sqlDB.endTransaction();
            }
            break;
        default:
            thrownewIllegalArgumentException("Unknown URI: " + uri);
        }
        // getContext().getContentResolver().notifyChange(uri, null);
    } catch (Exception e) {
      // Your error handling
    }

    return insertCount;
}

And in your code something like:

/**
 * Inserts new measurement information.
 * 
 * @param ArrayList of measurements
 * @return number of inserted entries
 */publicstaticlongbulkInsertEntries(ArrayList<Item_Measurement> readings) {
    // insert only if data is set correctlyif (readings.size() == 0)
        return0;

    longinsertCount=0;
    try {
        // insert new entries// ArrayList<ContentValues> valueList = new ArrayList<ContentValues>();
        ContentValues[] valueList = newContentValues[readings.size()];
        inti=0;
        for (Item_Measurement reading : readings) {
            ContentValuesvalues=newContentValues();
            values.put(COL_TIME_READING, reading.getTimeReading());
                            // ...
            valueList[i++] = values;
        }

        // returns ID
        insertCount = ContentProviderOwn.getAppContext().getContentResolver()
                .bulkInsert(ContentProviderOwn.MEASUREMENTS_URI_BASE, valueList);

    } catch (Exception e) {
        // Your error handling
    }
    return insertCount;
}

Solution 4:

I also use replace mode for insert row - db.insertWithOnConflict(EVENT_TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_REPLACE); Its will rid of conflict if record is exist already

In DatabaseHelper add UNIQUE INDEX

publicclassDataProviderextendsContentProvider {

    privatestaticclassDatabaseHelperextendsSQLiteOpenHelper {
        DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @OverridepublicvoidonCreate(SQLiteDatabase db){
            db.execSQL(CREATE_EVENT_TABLE);
            db.execSQL("CREATE UNIQUE INDEX event_idx ON " + EVENT_TABLE_NAME + " ( " + EventTable.EVENT_ID + " )");
// ...

        ...
        @OverridepublicintbulkInsert(Uri uri, ContentValues[] values) {
            Log.i(TAG, "bulkInsert");
            if (values.length == 0)
                return0;
            intinsertCount=0;
            try {
                switch (uriMatcher.match(uri)) {
                    case EVENT_LIST:
                        try {
                            db.beginTransaction();
                            for (ContentValues value : values) {
                                longid= db.insertWithOnConflict(EVENT_TABLE_NAME, null, value, SQLiteDatabase.CONFLICT_REPLACE);
                                if (id > 0)
                                    insertCount++;
                            }
                            db.setTransactionSuccessful();
                        } catch (Exception e) {
                            // Your error handling
                        } finally {
                            db.endTransaction();
                        }
                        break;
                    default:
                        thrownewIllegalArgumentException("Unknown URI " + uri);
                }
                getContext().getContentResolver().notifyChange(uri, null);
            } catch (Exception e) {
                Log.i(TAG, "Exception : " + e);
            }
            return insertCount;
        }

And call bulkInsert like this:

            ContentValues[] cvArr = new ContentValues[eventList.size()];
            long insertCount = 0;
            int i = 0;
            for (Event event : eventList) {
                ContentValues cv = new ContentValues();
                cv.put(DataProvider.EventTable.EVENT_ID, event.id);
                cv.put(DataProvider.EventTable.SENSOR_ID, event.sensor_id);
                cv.put(DataProvider.EventTable.TIMESTAMP, event.time);
                cvArr[i++] = cv;
            }
            // returns ID
            insertCount = context.getContentResolver()
                    .bulkInsert(DataProvider.CONTENT_EVENT_LIST, cvArr);

Post a Comment for "Multiple Rows Insert With Contentprovider"