SQL cursors in android

2019-05-30 23:59发布

问题:

Suppose I run some query and get a cursor which I want to use to update the entries in the database. What happens if the database is updated while the cursor is not closed? For example, suppose the cursor is pointing at the 1st entry in the result set and I run a query that updates the 10th element. Does the current cursor reflect those changes?

回答1:

The cursor is a copy of the results in memory, so you can modify the database at will. Since it's a copy, it won't reflect any changes - you'd need to requery for that.

However, it is absolutely imperative that you close it as soon as possible, to save memory and to make your app not crash when you switch activities.



回答2:

You need to call cursor.requery() in order to reload changes from the database.