0

I'm trying to create a local backup of a database from my application. In order to accomplish this, I created the following function in javascript:

$rootScope.CriarBackupDoBanco = function () {
        var fileTransfer = new FileTransfer();
        var url = 'file://mnt/sdcard/';
        var fileName = 'database_test.db';
        var store = cordova.file.dataDirectory;
        try {
            DBA.query('.backup file://mnt/sdcard/DBtest.db').then(function (result) {
                return DBA.getAll(result);
            });
        }
        catch (e) {
            console.log(e);
        }

Not sure if it will help, this is the query function of the DBA factory:


    // Handle query's and potential errors
    self.query = function (query, parameters) {
        parameters = parameters || [];
        var q = $q.defer();

        $ionicPlatform.ready(function () {
            DB.executeSql(query, parameters, function (result) {
                q.resolve(result);
            }, function (error) {
                console.warn('Error DBA:');
                error.DBA_query = query;
                error.DBA_parameters = parameters;
                console.warn(error);
                q.reject(error);
            });
        });
        return q.promise;
    }

However, it returns a syntax error, both with "backup" and ".backup". Changing "file://mnt/sdcard/DBtest.db" for "DBtest.db", showed no effect either.

I used as reference those sites:
How to backup sqlite database?

SQL script to "copy" a database

https://www.quackit.com/sqlite/tutorial/backup_a_database_to_file.cfm

Using window.resolveLocalFileSystemURL(path, OnSuccess(), OnError()), showed no results, neither success or fail, as if skipping it. (Perhaps a bug on cordova?).

I also confirmed that my DBA.query() function works by using commands like select, insert, create, delete.

My program should run on Android with API version 19 or newer. I am using Ionic 1. AngularJS and Cordova 9.0.0.

The error showed is: "Syntax error near ."(when using .backup) or "Syntax error near backup"(when using backup)

Fernando
  • 128
  • 1
  • 1
  • 9

1 Answers1

0

Not exactly what i wanted, but it works.

Using the SQLite Porter Cordova/Phonegap Plugin (https://www.npmjs.com/package/uk.co.workingedge.cordova.plugin.sqliteporter)

this plugin allows to export/import a database as JSON or SQL. So, although it dows not create a .db file as i desired it stills enable me to create a text to use as backup.

Fernando
  • 128
  • 1
  • 1
  • 9