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

copyDbToStorage #38

Closed rafaellop closed 7 years ago

rafaellop commented 7 years ago

My test device is Android 5.0 and I'm trying to utilize the copyDbToStorage() function to make a backup of my database, but each way I try the plugin reports error:

{"message":"/storage/sdcard1/Android/data/com.appbundleid: open failed: EISDIR (Is a directory)","code":400}

I call this function this way:

window.plugins.sqlDB.copyDbToStorage('appDB.db', 0, '/storage/sdcard1/Android/data/com.appbundleid/', 
   function() {
      console.log('SUCCESS')
   }, 
   function(e) {
      console.log('@@@@ ERROR ', JSON.stringify(e))
   });

Could you please give a working example of this?

an-rahulpandey commented 7 years ago

Hi, You have to give the db name in the destination path also. There was issue with the formatting of ReadMe, the params are explained there, have a look.

rafaellop commented 7 years ago

I have tried both the destination with the app database name and without (as in the example). In the first case the plugin creates a folder on the storage with the db name. This is supposedly not correct?

an-rahulpandey commented 7 years ago

Thanks for debugging it. I also noticed the issue, I did a quick direct edit on Github, can you download the plugin from Github and test it? Make sure you use this code -

window.plugins.sqlDB.copyDbToStorage('appDB.db', 0, '/storage/sdcard1/Android/data/com.appbundleid/', 
   function() {
      console.log('SUCCESS')
   }, 
   function(e) {
      console.log('@@@@ ERROR ', JSON.stringify(e))
   });

The / at the end of destination is very important.

rafaellop commented 7 years ago

Just tested and there are some build errors. For me it seems the variable is declared but maybe it must be initialized first. Here's the log. I\m on Cordova 6.5.0 :

:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
\platforms\android\src\me\rahul\plugins\sqlDB\sqlDB.java:168: error: vari
able destFolder might not have been initialized
        if(!destFolder.exists()){
            ^
Note: Some input files use or override a deprecated API.
:compileDebugJavaWithJavac FAILED

BUILD FAILED

Total time: 1.982 secs
Note: Recompile with -Xlint:deprecation for details.
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Error: cmd: Command failed with exit code 1 Error output:
\platforms\android\src\me\rahul\plugins\sqlDB\sqlDB.java:168: error: vari
able destFolder might not have been initialized
        if(!destFolder.exists()){
            ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
rafaellop commented 7 years ago

Quick fix is to use

File destFolder = new File(dest);

for the initialization and it works then. Not sure if this is bullet proof.

rafaellop commented 7 years ago

OK, here's a bullet proof version. Also sending a change to the repo + pr #39 :

        File source = cordova.getActivity().getDatabasePath(dbname);
    File destFolder;
    File destination;

        if(dest.indexOf("file://") != -1){
            destination = new File(dest.replace("file://","") + dbname);
        } else {
            destination = new File(dest + dbname);
        }

        destFolder = new File(dest);

        if(!destFolder.exists()){
            destFolder.mkdirs();
        }

        if(!destFolder.exists()) {
            sendPluginResponse(404, "Invalid output DB Location", true, callbackContext);
            return;
        }

        if(source.exists()) {
            this.newCopyDB(source,destination,callbackContext);
        } else {
            sendPluginResponse(404, "Invalid DB Location or DB Doesn't Exists", true, callbackContext);
        }
    }
an-rahulpandey commented 7 years ago

Thanks for the pull request, I have merged with little changes. Please test it. While you are at it, can you please also test the copyDbFromStorage function?

rafaellop commented 7 years ago

I'm doing this at the moment. Trying to first export the db and then import by closing the current, removing the current, copying from storage and opening again. I'll let you know.

rafaellop commented 7 years ago

OK, the process works. I was able to backup my db to the path and then import it on another device. The copyDbFromStorage() and copyDbToStorage() seems to work correctly. However, I've got another issues regarding the logic of the backup/restore process and I'll send in another issues open (#40). I hope you don't mind and I thank you very much for being such a responsive developer.

an-rahulpandey commented 7 years ago

Thanks for the support. Love to help whenever I can.