10

One of my methods returns a Cursor from some SQLite query results.

As I'm navigating through the cursor, there are some records I want to change/update. Can I update directly with the cursor? Or do I have to manually UPDATE using the record ID from the cursor?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
  • i know it's late , but i need to understand what do you want exactly , you want to update some record in database and update their values in the cursor without making a re-query to db ? – confucius May 04 '12 at 22:24
  • Yes. However I misunderstood the way cursors work when I asked this question. – Jake Wilson May 04 '12 at 22:38
  • i made solution , where i have a 5k row in cursor that get displayed in a list view now if the user click on a button i have to make an update to database and modify the cursor value since a UI element depend on that , i made this and i avoided the requery issue by modify the cursor values and make the update to database . is that good ,or not ? – confucius May 04 '12 at 22:56
  • one can chnage http://stackoverflow.com/questions/3609126/changing-values-from-cursor-using-simplecursoradapter – Shirish Herwade Dec 09 '15 at 12:55

3 Answers3

5

You can not directly update records with the cursor. Read the Android Cursor doc.

Lycha
  • 9,937
  • 3
  • 38
  • 43
  • I think, one can chnage http://stackoverflow.com/questions/3609126/changing-values-from-cursor-using-simplecursoradapter – Shirish Herwade Dec 09 '15 at 12:56
  • 1
    @ShirishHerwade To me it seems the linked discussion doesn't talk about writing anything to database. It just offers solution to filter/transform the data that is read. – Lycha Jan 05 '16 at 08:15
3

You need to implement a Content Provider that allows to update the record, in short you need to override the update function in your ContentProvider class.

public int update(Uri uri, ContentValues values, String where, String[] whereArgs)

In short you'll have to update them, this is not done directly from the data received in the Cursor.

This and this link should help.

SpeedBirdNine
  • 4,610
  • 11
  • 49
  • 67
0

To do that the best way is to implement a content provider for your data http://developer.android.com/guide/topics/providers/content-providers.html

You can use framework like datadroid for that http://datadroid.foxykeep.com/

Mathieu Hausherr
  • 3,485
  • 23
  • 30