Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Can you update a Cursor for SQLite results?

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?

like image 860
Jake Wilson Avatar asked Nov 01 '11 22:11

Jake Wilson


People also ask

Does SQLite support cursors?

The sqlite3. Cursor class is an instance using which you can invoke methods that execute SQLite statements, fetch data from the result sets of the queries. You can create Cursor object using the cursor() method of the Connection object/class.

What is cursor in SQLite in Android?

Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.

What is the cursor moveToFirst in Android?

moveToFirst() method moves the cursor to the first row.


2 Answers

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

like image 167
Lycha Avatar answered Nov 01 '22 10:11

Lycha


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.

like image 24
SpeedBirdNine Avatar answered Nov 01 '22 10:11

SpeedBirdNine