Reputation: 3
I am using SQLite3 in my Android App for data storing from Bluetooth LE. There are two occurrences when the SQLite is used:
My ask is: Is this the correct solution? Is it possible to let the connection open? I won't delete the last stored data by some system cleaner for example. Also, I want to see all of my data during app development. I call closing connection in onDestroy method in service but I read that it is not recommended for release.
@Override
public void onDestroy() {
if(writableTestbedDb != null){
writableTestbedDb.close();
}
if(testbedDbHelper != null) {
testbedDbHelper.close();
}
Log.w(TAG, "SERVICE DESTROYED");
this.stopSelf();
super.onDestroy();
}
I using a singleton design pattern for database connection.
public static TestbedDbHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new TestbedDbHelper(context.getApplicationContext());
}
mInstance.setWriteAheadLoggingEnabled(false);
return mInstance;
}
Upvotes: 0
Views: 1851
Reputation: 723
This happens because you use write-ahead logging. I see that you have the code to disable it, but somehow you still use it. Nevertheless, these files (wal & shm) are OK generally and there is nothing wrong with them. But if you want to make sure that you have only one file (.db), you can execute somewhere in your code:
pragma wal_checkpoint
to ensure everything is cleared. You can do it, for example, right after you open your db instance. This will save all the data that remained unsaved from the previous time - to the main file.
Upvotes: 1