I blogged about this last month (http://www.raymondcamden.com/index.cfm/2014/2/24/Creating-a-data-backup-of-a-WebSQL-database), but since SO frowns on just sharing solutions, I'll post some of the code here. ;) The basic premise is:
You go through each table. You select *. You take the rows of data and then you convert them to a string.
Here is code I used to get the data.
function backup(table) {
var def = new $.Deferred();
db.readTransaction(function(tx) {
tx.executeSql("select * from "+table, [], function(tx,results) {
var data = convertResults(results);
console.dir(data);
def.resolve(data);
});
}, dbError);
return def;
}
$(document).on("click", "#doBackupBtn", function(e) {
e.preventDefault();
console.log("Begin backup process");
$.when(
backup("notes"),
backup("log")
).then(function(notes, log) {
console.log("All done");
//Convert to JSON
var data = {notes:notes, log:log};
var serializedData = JSON.stringify(data);
console.log(serializedData);
});
});
The very last two lines are what you would tweak. For me I wanted JSON so I used native JSON serialization. If you wanted SQL you would need to loop over the lines of data and generate INSERT statements. You may be better off doing that on the server side though.
I forgot to include this utility. It simply converts SQLResultSet objects into an easier array.
//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;
}