Reputation: 23
I have created a "backup system", to copy all the table records from a sqlite database, where I take those records and create a .json file. The download in browsers works perfectly, but on android I can't.
#1 - I open a connection to the database.
#2 - Treat errors.
#3 - Give a SELECT on all DB tables.
#4 - Create Backup (agora os registros em .json, Create the file with the database backup)
#5 - Generic utility (convertResults)
// Code to Create Backup
$(document).ready(function() {
console.log("Application begun")
var db;
// → #1 Open connection with the die bank
db = window.openDatabase("DB_registry", "1.0", "DB_registry", 10485760)
// → #2 Treat errors
//Generic error handler
function dbError(e) {
console.log("SQL ERROR");
console.dir(e);
}
// → #3 give a SELECT on all DB tables
function backup(table) {
var def = new $.Deferred();
db.readTransaction(function(tx) {
tx.executeSql("select * from "+table, [], function(tx,results) {
console.log('↓ SQLResultSetRowList ↓')
console.log(results)
var data = convertResults(results)
def.resolve(data)
});
}, dbError);
return def;
}
// → #4 Create Backup
$(document).on("click", "#doBackupBtn", function(e) {
e.preventDefault();
console.log("Begin backup process");
$.when(
backup("tb_registry"),
backup("tb_courses"),
).then(function(tb_registry, tb_courses) {
// console.log("All done")
//Convert to JSON
var data = {tb_registry:tb_registry, tb_courses:tb_courses}
var serializedData = JSON.stringify(data, null, 2)
// console.log(serializedData)
$('#showRegistry').text(serializedData)
var obj = data
var data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj));
//!Create the file with the database backup
var a = document.createElement('a');
a.href = 'data:' + data;
a.download = 'data.json';
a.innerHTML = 'download JSON';
var container = document.getElementById('container');
container.appendChild(a);
})
})
//Generic utility
function convertResults(resultset) {
var results = []
for(var i=0,len=resultset.rows.length;i<len;i++) {
var row = resultset.rows.item(i)
var result = {}
for(var key in row) {
result[key] = row[key];
}
results.push(result);
}
return results;
}
})
Upvotes: 1
Views: 110
Reputation: 11
This is not possible without cordova-plugin-file
! You need to add additional logic for downloading from cordova app into other places, like download folder.
Upvotes: 1