0

I have an open database an read out some data. After my read operations are done I close the database but not the cursor.

Do I need to close the Cursor too? I assume it will be done by the close of the db, but is that true? What may happen if I don't close the Cursor?


SQLiteDatabase db = ...;
Cursor c = database.rawQuery("SELECT * FROM foo");
//read some values using the Cursor

c.close(); //<- is this required?
db.close();
Chriss
  • 5,157
  • 7
  • 41
  • 75

2 Answers2

0

The documentation says:

When finished iterating through results, call close() on the cursor to release its resources.

If you do not close the cursor, you might get an exception later.

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
0

If you close a database, the corresponding cursor will also be closed as far as I know.

An example of an exception that was thrown when I tried to traverse through a cursor (via cursor.moveToNext()) after having closed it's corresponding SQLite database is as follows:

java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

M.K
  • 1,464
  • 2
  • 24
  • 46
HJDesulme
  • 16
  • 6