Steel
Steel

Reputation: 255

ContentProvider Update

Is there a way to do the following query using the ContentProvider ?

UPDATE TableName 
SET ColumnName=ColumnName + 1 
WHERE id=1

I know I could execute this as a rawQuery but I'd like to know how many rows have been affected. Moreover, the execSQL method says :

It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert(String, String, ContentValues), update(String, ContentValues, String, String[]), et al, when possible.

However, I couldn't find any way to use the update method from my ContentProvider to do it.

Upvotes: 0

Views: 775

Answers (1)

Karakuri
Karakuri

Reputation: 38605

You could try this:

SQLiteDatabase db = ...; // get your database
String sql = "UPDATE tableName SET columnName = columnName + 1 WHERE id = 1";
SQLiteStatement statement = db.compileStatement(sql);
int affected = statement.executeUpdateDelete();
statement.close();

(begin edit)

You could define a URI that when passed to update simply ignores the ContentValues argument and executes the code above.

(end edit)

The only other way I can think of to do this through a ContentProvider would be to implement the call method in your ContentProvider and redirect to another method based on what gets passed in. Then you would use ContentResolver.call(...) and give it a Bundle with necessary information.

Upvotes: 3

Related Questions