Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copyDbToStorage #38

Closed
rafaellop opened this issue Mar 6, 2017 · 10 comments
Closed

copyDbToStorage #38

rafaellop opened this issue Mar 6, 2017 · 10 comments

Comments

@rafaellop
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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
Copy link
Owner

an-rahulpandey commented Mar 8, 2017

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
Copy link
Contributor Author

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
Copy link
Contributor Author

rafaellop commented Mar 8, 2017

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
Copy link
Contributor Author

rafaellop commented Mar 8, 2017

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
Copy link
Owner

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
Copy link
Contributor Author

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
Copy link
Contributor Author

rafaellop commented Mar 8, 2017

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants