lundi 25 avril 2016

Write and Read sqlite .db file in Phonegap by using FileSystem API

Hi I have a task to read the existing device sqlite .db file and write it in binary format and store the data into my server.. And whenever I need it for restore from server and I can replace data with device .db file. For POC(proof of concept) I have done it locally(stored .db file in sdcard and replaced the same with device existing .db file, its working properly BUT my problem is to read the .db file as a base64 mime type and stored it into server. PDF and images are I can store with specific mime type like image/jpeg, application/pdf. Trust me Folks I have been googling this couple of days But there is no any LUCK

Till now I have tried with below code

var dbPath;
 function doBackupFile() {
 var version = parseFloat(window.device.version);
 dbPath = cordova.file.applicationStorageDirectory
  +'/app_webview/databases/file__0/1';
  if(version < 4.4) {  
    dbPath = cordova.file.applicationStorageDirectory
   +'/app_database/file__0/0000000000000001.db';
  }        
  //alert("dbPath "+dbPath);
 window.resolveLocalFileSystemURL(dbPath,
        function(fs) {
            var parent = cordova.file.externalRootDirectory
                    + "Download/SMU/Backup";
            //var newName = "myDb.db";
             var newName = 'Backup_' + new Date().getTime();   
            window.resolveLocalFileSystemURL(parent, function(
                    directoryEntry) {
                fs.copyTo(directoryEntry, newName, function() {
                    alert("Backup Success");

                      readFile(newName);


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



function readFile(fileName, firstTime) {
console.log("readFile "+fileName);
 var defered = $.Deferred();
console.log('Start reading the file ::: ' + fileName); 
 window.resolveLocalFileSystemURL
(cordova.file.externalRootDirectory + '/' + 
backupFolder + '/' + fileName, function(fileEntry){
console.log('Got File Entry...');
fileEntry.file(function(file){
  console.log('Reading the file...');
  var reader = new FileReader();
  reader.onloadend = function(e) {

  var data_db = e.target.result;
  console.log('File read completed - '+data_db);

    };     
   reader.readAsDataURL(file);
   }, onfail);
  });  
 function onfail(err){
  console.log('Error while reading the file ::: ' + JSON.stringify(err));
    return defered.reject(err);
}
return defered.promise();

}

I am reading the .db file as a readAsDataURL() method

Pls check the console I am getting File read completed -

File read completed - data:null;base64,U1FMaXRlIGZvcm1hdCAzAAQAAQEAQCAgAAAADgAAAFUAAAAAAAAAAAAAABoAAAAEAAAAAAAAAB8AAAABAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAC3mBwUAAAAIA9gAAAAAKwP7A/YD8QPsA+cD4gPdA9gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKBsAAAAnGAAAACUTAAAAIQ8AAAAjDAAAACoJAAAAJAcAAAAiBQEAAAAAAQAAAAABAAAAAAEAAAAAAQAAAAABAAAAAAEAAAAAAQAAAAABAAA

it continues upto 120098 lines, I have pasted here only 2% of console because SO body is limited to 3000 characters only .. I don't know weather this binary formated data is valid .db file or not. And also I am getting data:null at the beginning, for image and pdf we are getting their respective mime types.. Please anybody explain where I am making mistakes for .db files

Aucun commentaire:

Enregistrer un commentaire