androidcordovawindows-10-universalcordova-plugin-filecordova-sqlite-storage

Using cordova-plugin-file to copy SQLite database cordova-sqlite-storage in Windows 10


I need some help with cordova-plugin-file to backup a database used by cordova-sqlite-storage in Windows 10 App. My Code works fine on Android platform. Database can be copied in both directions (backup and restore). Database used by the App seems to be the following:

C:\Users\myuser\AppData\Local\Packages\io.cordova.hellocordova_h35559jr9hy9m\LocalState\sample.db

The App tries to use this Location to locate the file, but it's not found:

ms-appx:///databases/sample.db

How do I get the right location for Windows to copy the file?

This is the Code I use to copy the file:

backupDatabase(name: string): Promise {

return new Promise<boolean>((resolve, reject) => 
{
  var fileName: string = window.cordova.file.applicationStorageDirectory + 'databases/' + name;
  var directoryName: string = window.cordova.file.externalRootDirectory;

  window.resolveLocalFileSystemURL(fileName, (file: FileEntry) => {
    console.log('[!] Database exists: ' + fileName);
    console.log('[!] Storage: ' + directoryName);
    window.resolveLocalFileSystemURL(directoryName, (directory: DirectoryEntry) => {
      console.log('[!] Directory: ' + directory.toURL());
      directory.getDirectory("Backup", {create: true, exclusive: false}, (directoryBackup: DirectoryEntry) => {
        console.log('[!] Directory: ' + directoryBackup.toURL());
        file.copyTo(directoryBackup, name, (copiedFile: Entry) => {
          console.log('[!] Copy success');
          resolve(true);
        }, (error: FileError) => {
          console.log('[!] Copy failed: ' + error.code);
          reject(error);
        });
      }, (error: FileError) => {
        console.log('[!] Backup Directory not found: ' + directoryName + 'Backup' + ' errorcode: ' +  + error.code);
        reject(error);
      })
    }, (error: FileError) => {
      console.log('[!] Directory not found: ' + directoryName + ' errorcode: ' +  + error.code);
      reject(error);
    }); 
  }, (error: FileError) => {
      console.log('[!] Database not found: ' + fileName + ' errorcode: ' +  + error.code);
      reject(error);
  });
});

Solution

  • The App tries to use this Location to locate the file, but it's not found:

    ms-appx:///databases/sample.db

    How do I get the right location for Windows to copy the file?

    Your are using the wrong scheme ms-appx:/// refers to the package installed location.

    For C:\Users\myuser\AppData\Local\Packages\io.cordova.hellocordova_h35559jr9hy9m\LocalState\sample.db

    you need to use ms-appdata:///local/sample.db.