Reputation: 6076
I have following code:
Cursor cursor = mDB.rawQuery(query, null);
cursor.moveToFirst();
do {
//do stuff
} while (cursor.moveToNext());
cursor.close();
query = "<some sql code>";
cursor = mDB.rawQuery(query, null);
Upvotes: 0
Views: 84
Reputation: 2792
The documentation about close()
says the you would not be able to use the cursor again:
Closes the Cursor, releasing all of its resources and making it completely invalid. Unlike deactivate() a call to requery() will not make the Cursor valid again.
If you are using the cursor in an adapter you can close the old cursor after you set the new one. swapCursor()
returns the old Cursor, or returns null if there was not a cursor set, also returns null if the if you try to swap the same instance of the previously set cursor. Knowing that, you can try something like this:
Cursor c = adapter.swapCursor(cursor);
if(c != null)
c.close();
Upvotes: 0
Reputation: 93728
You can do that, but it isn't doing what you seem to think it is. You aren't reopening the cursor. You're setting the variable to reference a new cursor for the new query.
I've seen null returns from db queries. I'd keep the null check. The amount of overhead is minimal.
Upvotes: 1