Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flushing and SQLite database on android

Tags:

android

sqlite

I have an android application using an SQLite database. I open the database when the application starts, but never close it as it is in constant use.

What is the best way to tell the database to flush all its changes to permanent storage? Do I need to just close it and re-open or is there a more efficient way?

My problem is that when testing on a phone, turning the phone off after a number of writes sometimes causes the database to loose the most recent updates when the application is restarted, which is obviously unacceptable for a database system.

Since I cannot find out how to capture the application close event I cannot know when to manually close the database.

like image 419
Akusete Avatar asked Jul 13 '10 00:07

Akusete


People also ask

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.

Is SQLite good for Android?

SQLite Database is an open-source database provided in Android which is used to store data inside the user's device in the form of a Text file. We can perform so many operations on this data such as adding new data, updating, reading, and deleting this data.

Where is the SQLite database stored 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

I open the database when the application starts, but never close it as it is in constant use.

Don't do that. You are apparently ignoring all the errors that are appearing in LogCat complaining that you are leaking database connections.

What is the best way to tell the database to flush all its changes to permanent storage?

It will do that automatically at the end of a transaction. By default, each individual SQL operation (e.g., insert) is a transaction.

Do I need to just close it and re-open or is there a more efficient way?

You should close your database at some point (e.g., onDestroy() of the service that is mediating your database).

My problem is that when testing on a phone, turning the phone off after a number of writes sometimes causes the database to loose the most recent updates when the application is restarted, which is obviously unacceptable for a database system.

If you can create a sample project that demonstrates the problem even after you are properly closing your database connection, post it to the Android issue tracker.

Since I cannot find out how to capture the application close event I cannot know when to manually close the database.

Close it when all activities have unbound from the service that is mediating the database connection, triggering that service's onDestroy() method.

Or, open a fresh connection in each component that needs access to the database, and use Java synchronization to ensure two threads do not try to simultaneously use the database (if needed).

like image 136
CommonsWare Avatar answered Oct 02 '22 20:10

CommonsWare