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?
Once created, the data base will remain in existence until you explicitly delete it or until the app is uninstalled.
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.
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.
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.
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?
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