My code is like below:
Cursor getResults() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, null, null,
null, null, null);
db.close();
return c;
}
My question is, after db.close() is executed, is the cursor c still alive and navigable?
Thanks.
Not closing a cursor will keep locks active that it holds on the rows where it is positioned.
Cursors are used by database programmers to process individual rows returned by database system queries. Cursors enable manipulation of whole result sets at once. In this scenario, a cursor enables the sequential processing of rows in a result set.
Yes, it's recommended to close the cursor when you are done using that cursor object so that cursor can do whatever house keeping work it wants to do upon closure.
You can close the cursor once you have retrieved the values for that particular object inside your method. btw... You don't have to recreate a listview every time for a user click event. Just notify that there is some change in data of your adapter that has been set on the listview.
What is a Database Cursor? A database cursor can be thought of as a pointer to a specific row within a query result. The pointer can be moved from one row to the next. Depending on the type of cursor, you may be even able to move it to the previous row.
Think of it this way: a SQL result is like a bag, you get to hold a whole bunch of rows at once, but not any of them individually; whereas, a cursor is like a pair of tweezers. With it, you can reach into the bag and grab a row, and then move onto the next.
The cursor can only move to the next row in the result. SCROLL – the cursor can use operations, such as FIRST, LAST, PRIOR, NEXT, RELATIVE, ABSOLUTE to navigate the results. When we talk about data sensitivity we mean what happens when the same row is changed by someone else?
In this case we can use the “WITH HOLD” clause during the cursor declaration. The “WITH HOLD” clause will keep the cursor open even after firing the COMMIT statement. We can give the “WITH HOLD” clause in the following way.
No. You do not want to use a cursor while the database is closed. When you call close()
, it makes the object (and it's corresponding cursor) invalid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With