Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Can SQLite Cursor's be used after closing the database?

First of all, correct me if I'm wrong, but if you close a database connection, you can't use the Cursor you got from it, correct?

db.open();
Cursor c = db.query(true, "MyTable", columns, null, null, null, null, null, null);
db.close();

// The Cursor is empty now because the db was closed...
c.moveToNext();
Log.v(TAG, c.toString(0));

So is it there any way to use the Cursor after closing the database? Like is there any way to pass it elsewhere and use it kinda like an object? Or do you always have to leave the database connection open until you are done with the cursor?

like image 954
Jake Wilson Avatar asked Nov 01 '11 21:11

Jake Wilson


People also ask

Is SQLite database permanent?

Once created, the data base will remain in existence until you explicitly delete it or until the app is uninstalled.

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.

How can I recover SQLite database in Android?

We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data. This method return the total number of columns of the table.

Where does SQLite store data in Android?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.


1 Answers

No, As it says in the link below, if you close the database, the cursor becomes invalid.

Will cursor be still alive after database is closed?

like image 76
Todd Davies Avatar answered Oct 07 '22 04:10

Todd Davies