Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export sqlite db in phonegap app

Tags:

sqlite

cordova

We have an app using cordova (phonegap) and his own sqlite DB (I mean we didn't install an external sqlite).

We need to backup the DB into the SD memory but we have problems to export the sqlite file from phonegap to SD.

May anybody help us?

like image 660
alvaizq Avatar asked Jul 25 '12 11:07

alvaizq


2 Answers

A couple years late, I know, but this is working for me on Cordova 5.4.1 and running on Android (I think iOS uses cordova.file.application too, but I have not tested yet):

this.getdbfilename = function () {
    return "dbfile.db";
};

this.getdbdirectory = function() {
    return cordova.file.applicationStorageDirectory + "databases/";
};


// copy DB file out to non-private app directory.
this.copyDBFileOut = function (outfilename) {
    window.resolveLocalFileSystemURL(this.getdbdirectory() + this.getdbfilename(), function (fileEntry) {
        window.resolveLocalFileSystemURL((cordova.file.externalDataDirectory || cordova.file.documentsDirectory), function(dirEntry) {
            fileEntry.copyTo(dirEntry, outfilename, function() { console.log("copyDBFileOut() succeeded");}, this.errorHandler);
        });
      });
  };
like image 90
Edward Crane Avatar answered Nov 15 '22 06:11

Edward Crane


This sounds good with Cordova 3.5 and org.apache.cordova.file 1.2.1

Some variations needed for differents path.

window.resolveLocalFileSystemURL("file:///data/data/my-app-name/databases/name-of.db", function(fs) {
                var parent = "file://mnt/external_sd/";
                var newName = "mybackup.db";
                window.resolveLocalFileSystemURL(parent, function(directoryEntry) {
                    fs.copyTo(directoryEntry, newName, function() {
                        alert("Backup ok");
                    }, failFiles);
                });
            }, failFiles);

function failFiles(error) {        
  if (error.code == FileError.NOT_FOUND_ERR) alert("Message : NOT_FOUND_ERR" )
  else if (error.code == FileError.SECURITY_ERR) alert("Message : SECURITY_ERR" )
  else if (error.code == FileError.ABORT_ERR) alert("Message : ABORT_ERR" )
  else if (error.code == FileError.NOT_READABLE_ERR) alert("Message : NOT_READABLE_ERR" )
  else if (error.code == FileError.ENCODING_ERR) alert("Message : ENCODING_ERR" )
  else if (error.code == FileError.NO_MODIFICATION_ALLOWED_ERR) alert("Message : NO_MODIFICATION_ALLOWED_ERR" )
  else if (error.code == FileError.INVALID_STATE_ERR) alert("Message : INVALID_STATE_ERR" )
  else if (error.code == FileError.SYNTAX_ERR) alert("Message : SYNTAX_ERR" )
  else if (error.code == FileError.INVALID_MODIFICATION_ERR) alert("Message :  INVALID_MODIFICATION_ERR" )
  else if (error.code == FileError.QUOTA_EXCEEDED_ERR) alert("Message : QUOTA_EXCEEDED_ERR" )
  else if (error.code == FileError.PATH_EXISTS_ERR) alert("Message : PATH_EXISTS_ERR" )  
}  
like image 27
Michele Avatar answered Nov 15 '22 04:11

Michele