an-rahulpandey / cordova-plugin-dbcopy

Copy SQLite Database from www folder to default app database location
Apache License 2.0
89 stars 47 forks source link

copyDbFromStorage seems to fail for Android 9 #54

Closed hohae closed 5 years ago

hohae commented 6 years ago

I've done some tests with the brand new Android 9. According to the logs, everything is fine. But when sqlite opens the DB, it's empty. This seems to be the default behaviour of sqlite to create a new db if the db is not found during opening. This makes me think that the copyDbFromStorage does not copy the file.

Any ideas of how to debug that? It works fine from Android 4.3 to 8.1. It just fails on Android 9.

an-rahulpandey commented 6 years ago

can you post some code here, how you are doing that?

hohae commented 6 years ago

Hi,

i was able to fix it by adding the following line:

public class DatabaseHelper extends SQLiteOpenHelper { … public void createdatabase(File dbPath, String source, final CallbackContext callbackContext) throws IOException {

         Log.d("CordovaLog","Inside CreateDatabase getAbsolutePath= "+dbPath.getAbsolutePath());
         Log.d("CordovaLog","Inside CreateDatabase path = "+dbPath.getPath());
         databasePath = dbPath.getAbsolutePath();

         this.getReadableDatabase();
         this.close();

         try {
                copyDatabase(dbPath, source, callbackContext);
         } catch (IOException e) {
                throw new Error(
                             "Create Database Exception ============================ "
                                          + e);
         }

   }

Regards, Holger

From: Rahul Pandey notifications@github.com Sent: Monday, 27 August 2018 14:34 To: an-rahulpandey/cordova-plugin-dbcopy cordova-plugin-dbcopy@noreply.github.com Cc: Holger Häffelin Holger.Haeffelin@arcworx.net; Author author@noreply.github.com Subject: Re: [an-rahulpandey/cordova-plugin-dbcopy] copyDbFromStorage seems to fail for Android 9 (#54)

can you post some code here, how you are doing that?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/an-rahulpandey/cordova-plugin-dbcopy/issues/54#issuecomment-416211491, or mute the threadhttps://github.com/notifications/unsubscribe-auth/APPmUVEuNd9kguBHrqoUi5jIIjHAseBgks5uU-c4gaJpZM4V6xb_.

an-rahulpandey commented 6 years ago

Hmm...I have added the close and open functionality in the copy function, is your databasehelper.java file exactly like this or it is from old version - https://github.com/an-rahulpandey/cordova-plugin-dbcopy/blob/master/src/android/DatabaseHelper.java

hohae commented 6 years ago

Yes, it’s the latest version and I’ve just added the close() after the getReadableDatabase() call.

The problem seems to be that if you call getReadableDatabase() and then try to overwrite the database before closing, this does not work any more under Android 9. The DB opens correctly but it is the empty DB, not the copied one. Unfortunately, this does not throw an exception while copying the file.

See also this description: https://stackoverflow.com/questions/50476782/android-p-sqlite-no-such-table-error-after-copying-database-from-assets. It led me to the solution.

From: Rahul Pandey notifications@github.com Sent: Tuesday, 28 August 2018 09:01 To: an-rahulpandey/cordova-plugin-dbcopy cordova-plugin-dbcopy@noreply.github.com Cc: Holger Häffelin Holger.Haeffelin@arcworx.net; Author author@noreply.github.com Subject: Re: [an-rahulpandey/cordova-plugin-dbcopy] copyDbFromStorage seems to fail for Android 9 (#54)

Hmm...I have added the close and open functionality in the copy function, is your databasehelper.java file exactly like this or it is from old version - https://github.com/an-rahulpandey/cordova-plugin-dbcopy/blob/master/src/android/DatabaseHelper.java

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/an-rahulpandey/cordova-plugin-dbcopy/issues/54#issuecomment-416473266, or mute the threadhttps://github.com/notifications/unsubscribe-auth/APPmUZ-paRerX6bNqNFEVdQzFY-iP9b2ks5uVOqYgaJpZM4V6xb_.