如何在phonegap中替换设备db文件

时间:2016-03-12 10:14:44

标签: android sqlite cordova filesystems phonegap-plugins

您好我正在获取设备db文件并存储在服务器中以备份我的应用程序数据库的要求是,如果设备丢失或丢失或被盗,那么在这种情况下用户可以从服务器下载数据库的备份可以将其恢复到他们的设备中。我相信下面的代码给了我一个带有文件名的指定路径的设备db文件。

function doBackup(){
 var defered = $.Deferred();
var version = parseFloat(window.device.version);
    dbPath = cordova.file.applicationStorageDirectory
      +'/app_webview/databases/file__0/1';

     if(version < 4.4) {  

      dbPath =  cordova.file.applicationStorageDirector+'/app_database
     /file__0/0000000000000001.db';
   }  
var fileName = 'Backup_' + new Date().getTime();   

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
   fs.root.getDirectory(backupFolder, {
    create : true,  
    exclusive : false  
       }, function(dir) {      
   window.resolveLocalFileSystemURL(dbPath, function(db) {
      db.copyTo(dir, fileName, function() {
  if(online) {      
     console.log("Im online - "+dbPath);
     readFile(fileName);
     //checkIfDBFileExists(backupFolder+'/log1.txt');
  } else {      
    var data = new BackupData();
    data.FileName = fileName;
    data.BackupDate = new Date().getTime();
    SaveBackupData(data, GetBackupData); 
  }        
   }, onfail)
   });  
   }, onfail);
    }, onfail);

   function onfail(err){
      return defered.reject(err);
     }
 return defered.promise();
     }

为了替换我正在尝试下面的代码但是没有运气

     function  copyFileToDB(fName){  
  var defered = $.Deferred();

// request for file system
 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs){
    //request for directory Entry
    //backup folder name in dd mm yyyy format
    var today = new Date();
    //var fileName = today.getDate()+" " + (today.getMonth()+1) + " "+today.getFullYear();
    var fileName = fName;

    fs.root.getDirectory("Download/SMU/Backup/", { 
        create: false,
        exclusive: false
    }, function(destDirectoryEntry)  
 {window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory 
   +"app_webview/databases/file__0/1",function(fileEntry){     
   fileEntry.copyTo(destDirectoryEntry, fileEntry.name, function(){
                alert("success - "+JSON.stringify(destDirectoryEntry));
            }, onfail);
        },onfail)
    }, onfail);
}, onfail);

function onfail(err){
    return defered.reject(err); 
}
return defered.promise();   
}

请有人帮我解决这个问题。

0 个答案:

没有答案