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

Issue with Android copyDbFromStorage #52

Closed bryans99 closed 6 years ago

bryans99 commented 6 years ago

Yes, I know you say it's untested.

I have a use case where I need to download some largish pre-populated databases on a regular basis. The databases get saved to the file system using cordova file and then dbcopy copyDbFromStorage puts it into the database directory. sqlite-storage is being used to read from the database.

Anyway, it seems to work okay on IOS but fails on Android. The copy works but there is no data in the database after it gets opened. After some debugging I believe it's related to a line of code in DatabaseHelper.createdatabase. Commenting out the call to getReadableDatabase() seems to solve the problem. Not sure what the purpose of that call is but perhaps you can consider it as a fix. ` public void createdatabase(File dbPath, String source, final CallbackContext callbackContext) throws IOException {

    // Log.d("CordovaLog","Inside CreateDatabase = "+dbPath);

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

`

an-rahulpandey commented 6 years ago

Hi, If you read here - https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper

SQLiteOpenHelper requires to open the db in readable or writable format first before performing an action first. I do not remember why exactly I try to open the database before copying it, rather than directly copying it to native location.

I will create one test copy of the plugin which just copy the db whenever I get time.

bryans99 commented 6 years ago

As near as I can tell, dbcopy is not aware of sqlite-storage. I am using sqlite-storage to control access to the databases (open, close, delete etc). dbcopy is just being used to copy from the location where the database is downloaded to, to the location where the databases are stored. The advantage of dbcopy is that it figures out the location of the databases. Prior to the copy, I close and delete the file using sqlite-storage.

I figured out what was happening by using the built in Android database classes (androidDatabaseImplementation: 2). When using this implementation three files are created in the database directory when the database is opened.

-rw------- 1 u0_a81 u0_a81 69632 2018-06-19 17:40 Code.db -rw------- 1 u0_a81 u0_a81 32768 2018-06-19 17:40 Code.db-shm -rw------- 1 u0_a81 u0_a81 412032 2018-06-19 17:40 Code.db-wal

After using copyDbFromStorage, I noticed the same three files being created. sqllite-storage is not aware of the database being opened so I cannot use it to close the database. When I open the database using sqllite-storage, the database appears to be empty (although on the file system, the size does not change). As soon as I close the database, the file is truncated.

I guess an alternate possibility is to close the database after the copy. If I get a chance I will try that today.

Thanks.

an-rahulpandey commented 6 years ago

@bryans99 Yes, thats a good observation, closing the database after opening in the plugin might do the trick. Let me see if I can quickly do it.