crbin1
crbin1

Reputation: 2229

Android update multiple rows with different values

I have a table with 3 rows _id, name and number. I have to update the column number of every row with different values. Actualy my code is:

ContentValues values;
ContentResolver cr = getContentResolver();
int n[] = new int[10];
// functions to assign the values to n[]
for(int i=1; i<31;i++) {
    values = new ContentValues();
    values.put("number", n[i]);
    cr.update(MyProvider.CONTENT_URI_TABLE, values, "_id="+i, null);
}

In this way I have to use a lot of UPDATE and multiple accesses to database. There is a way to semplify the procedure?

Upvotes: 3

Views: 1910

Answers (1)

wsanville
wsanville

Reputation: 37516

For this, I would use the applyBatch() method on ContentResolver. Basically, it takes an array of type ContentProviderOperation that specifies what you're updating/inserting/deleting.

While it won't simplify the code you're writing, it can be beneficial for the Content Provider that your are accessing. The benefit here is that the Content Provider could do something like apply these updates in a transaction.

Upvotes: 7

Related Questions