Skip to content

Instantly share code, notes, and snippets.

@keyz182
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyz182/11283385 to your computer and use it in GitHub Desktop.
Save keyz182/11283385 to your computer and use it in GitHub Desktop.
var totalProgress = 0;
var localFileName = 'test.sqlite';
var remoteFile = "http://www.example.com/test.sqlite"
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
console.log('file system retrieved.');
fs = fileSystem;
//Not exclusive, so success on create or open
var dir = fs.root.getDirectory('sql', {create:true},function(directory){
console.log(directory);
console.log(directory.toURL());
console.log(directory.toNativeURL());
//Exclusive, so success only is the file is newly created - i.e. skip download if file exists
var file = directory.getFile(localFileName, {create:true,exclusive:true}, function(file){
console.log(file);
console.log('DB does not exist');
console.log('Downloading sqlite file...');
ft = new FileTransfer();
ft.onprogress = function(evt) {
if (evt.lengthComputable) {
var progress = Math.floor((evt.loaded/evt.total)*100);
if(totalProgress!=progress){
//Convert to MB, then show two numbers after decimal point.
var totalMB = evt.total/(1024*1024);
totalMB = String(Math.round(totalMB * 100) / 100);
console.log("Downloading offline maps: " + progress + "% of " + totalMB + "MB");
totalProgress = progress;
}
} else {
console.log("+1");
}
};
//Use toURL to pass the correct file path format to ft
ft.download(encodeURI(remoteFile),file.toURL(), function (entry) {
console.log('download complete: ' + entry.fullPath);
//One of the following lines should work, I can't remember which.
var db = window.sqlitePlugin.openDatabase({name: "TestDatabase", bgType: 1, dburi=entry.toURL()});
//var db = window.sqlitePlugin.openDatabase({name: "TestDatabase", bgType: 1, dburi=entry.fullPath});
}, function (error) {
console.log('error with download', error);
});
}, function(error){
console.log(error);
});
}, function(error){
console.log("Error creating SQLite Diretory")
console.log(error);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment