在phonegap应用程序中导出sqlite db

时间:2012-07-25 11:07:13

标签: sqlite cordova

我们有一个使用cordova(phonegap)的应用程序和他自己的sqlite DB(我的意思是我们没有安装外部sqlite)。

我们需要将数据库备份到SD内存中,但是我们在将sqlite文件从phonegap导出到SD时遇到问题。

愿任何人帮助我们吗?

3 个答案:

答案 0 :(得分:1)

这对Cordova 3.5和org.apache.cordova.file 1.2.1

来说听起来不错

不同路径需要一些变化。

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" )  
}  

答案 1 :(得分:1)

迟了几年,我知道,但这对Cordova 5.4.1起作用并在Android上运行(我认为iOS也使用了cordova.file.application,但我还没有测试过):

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);
        });
      });
  };

答案 2 :(得分:0)

很久以前......但我认为这是解决方案:

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" )  
}  

function successDump() {    
}  

//Export DB. Get the file        file:///data/data/yourproject/app_database/file__0/0000000000000001.db  
//and save in the SD with the name yourproject.db
function copyBdtoSD() {    
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successDump, failFiles);            window.resolveLocalFileSystemURI("file:///data/data/yourproject/app_database/file__0/0000000000000001.db", copyFileToSd, failFiles);
}


function copyFileToSd(entry) {    
    var parent = "file:///mnt/sdcard",
        parentName = parent.substring(parent.lastIndexOf('/')+1),        
        parentEntry = new DirectoryEntry(parentName, parent);       
    var newName = "yourproject.db";

   // copy the file
   entry.copyTo(parentEntry, newName, successExport, failFiles);    
}
相关问题