Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will cursor be still alive after database is closed?

Tags:

android

sqlite

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.

like image 907
Kai Avatar asked Jun 28 '11 21:06

Kai


People also ask

What happens if you don't close a cursor?

Not closing a cursor will keep locks active that it holds on the rows where it is positioned.

What does cursor do in database?

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.

Why do we close the cursor?

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.

When should I call cursor close?

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?

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.

What's the difference between a cursor and a result in SQL?

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.

Can the cursor only move to the next row?

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?

How to keep the cursor open after firing the COMMIT statement?

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.


1 Answers

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.

like image 160
DeeV Avatar answered Oct 02 '22 15:10

DeeV