Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Regular Contributor
bobbyboy1999
Posts: 59
Registered: ‎03-07-2011
My Device: Not Specified

Web DB backup

Hi,

 

I have a Web DB application using Webworks for the Playbook that I would like to be able to backup.  The only thing I could find on this site so far was a reference to the fact that Playbook apps are sandboxed, with each app storing data as follows:

 

appname/app/

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Using-Persistence-Store-via-SQLit...

 

One of the problems, however,  is that the above link is for Air and not Webworks, although I'm not sure if there is any difference for storage between the two SDKs.

 

In any event, I really need to be able to backup my database.  Even though WebDB uses SQLite, the only backup function I could find was a .dump command for SQLite3, but I really don't think that applies to WebDB.

 

Any suggestions?  Even some idea of how to copy my database file (via Javascript of course) to another location as a backup in case the main file gets corupted would be helpful.

 

Thanks in advance.

Please use plain text.
Contributor
awjenning
Posts: 38
Registered: ‎08-06-2011
My Device: PlayBook
My Carrier: T-Mobile

Re: Web DB backup

I'll wake up this old post because I need the same thing.

 

In a nutshell I want to be able to take a copy of the SQLite database residing in the sandbox and place it in the shared documents folder so that  the user can back it up. Does anyone have an example?

 

I tried using the HTML5 filesystem api but it does not appear to be able to access the location where the database is stored. I hope that the device filesystem api can do this but I don't know where to begin.

 

Any help or pointers would be appreciated.

 

 

Please use plain text.
Contributor
rherfurth
Posts: 11
Registered: ‎04-20-2010
My Device: Z10
My Carrier: Fido

Re: Web DB backup

Same boat, bumping up

 

Would like to backup the database.

 

Same idea, 

 

1) Locate DB

2) Copy from location to say MISC/<app name> folder in filesystem, tag system file date

 

Then restore will look something like this

 

1) Copy from location say MISC/<app name> folder to location

 

When it is in MISC/<app name> user can copy over to Box / Dropbox or e-mail by themselves, as long as I make a copy for them to see.

Please use plain text.
Contributor
rherfurth
Posts: 11
Registered: ‎04-20-2010
My Device: Z10
My Carrier: Fido

Re: Web DB backup

Building post, to build solution.

 

Still researching find DB from within app, I think we can get it from SQL Lite DB itself for path, if not WE KNOW exact path already based off the sandbox 

 

Copy functions...

http://developer.blackberry.com/html5/apis/fileentry.html

 

See Copying an entry....

http://www.html5rocks.com/en/tutorials/file/filesystem/

 

 

So code will likely be something like 

 

Backup : 

Close DB

Locate DB (might be step 1)

Copy DB

Open DB

 

Restore: 

Locate Current DB

Close DB

Copy DB from 'backup folder'

Open DB

Please use plain text.
Contributor
rherfurth
Posts: 11
Registered: ‎04-20-2010
My Device: Z10
My Carrier: Fido

Re: Web DB backup

[ Edited ]

still chugging along...

 

function copy(cwd, src, dest) {
cwd.getFile(src, {}, function(fileEntry) {

cwd.getDirectory(dest, {}, function(dirEntry) {
fileEntry.copyTo(dirEntry);
}, errorHandler);

}, errorHandler);
}


function(fs) {
copy( '/accounts/1000/appdata/<app-name-dir>/data/webviews/database/', 'Databases.db', blackberry.io.sharedFolder +'/Misc/');
}

 

If I can get it to the Shared Folder, then use FileSystem to let the user backup to SD / Box / Drop Box...I think I can allow user to save the backup location as BB really made it slickly integrated

Please use plain text.
Contributor
awjenning
Posts: 38
Registered: ‎08-06-2011
My Device: PlayBook
My Carrier: T-Mobile

Re: Web DB backup

This is not a "copy, paste, it works" solution but  this works in my app for BB10 and PlayBook 

 

    //======================================================
    // Backup local database to shared storage
    //======================================================
    function backupDatabase() {
        progressOn();

        var FileName = "YourAppNameBkp_" + (new Date().getTime()) + ".db";
        var fromPath = util.device.webSQLPath() + "/0000000000000001.db";
        var toPath = util.device.sharedDocsPath();

        util.device.fileCopy(fromPath, toPath, FileName, function (RC, errDesc) {
            if (RC) {
                $cmdBackupDatabase.prev('span').find('span.ui-btn-text').text("Database backed up to: " + FileName); // For button types
                $cmdBackupDatabase.button("disable");
            } else {
                $cmdBackupDatabase.prev('span').find('span.ui-btn-text').text("Database Backup Error"); // For button types
            }
            progressOff();
        });
    }

    //======================================================
    // Restore local database from backup in local storage
    //======================================================
    function restoreDatabase(e) {
        var files = e.target.files;
        if (files.length > 0) {
            progressOn();

            var FileName = files[0].name;
            var fromPath = util.device.sharedDocsPath() + "/" + FileName;

            var toPath = util.device.webSQLPath();

            util.device.fileDelete(toPath + "/0000000000000001.db", function (RC, errDesc) {
                if (RC) {
                    util.device.fileCopy(fromPath, toPath, "0000000000000001.db", function (RC, errDesc) {
                        if (RC) {
                            $cmdRestoreDatabase.prev('span').find('span.ui-btn-text').text("Database restored from: " + FileName); // For button types
                            $cmdRestoreDatabase.button("disable");
                        } else {
                            $cmdRestoreDatabase.prev('span').find('span.ui-btn-text').text("Restore Failed"); // For button types
                        }
                        progressOff();                     
                    });
                }
            });
        }
    }




                webSQLPath: function () {
                    if (util.device.isBlackBerry()) {
                        var sandboxpath = util.device.sandBoxPath();
                        if (util.device.isBB10) {                         
                            return sandboxpath + "/webviews/database/local__0";
                        } else {
                            return sandboxpath + "/database/local__0";
                        }
                    } else if (util.device.isCordova()) {
                    } else {
                    }
                },

                sandBoxPath: function () {
                    if (util.device.isBlackBerry()) {
                        if (util.device.isBB10) {
                            return blackberry.io.home;
                        } else {
                            // Data Dir for this app ("file:///accounts/1000/appdata/YourAppName6cbcbf112f94ccc2f8ba2eec936a.testDev_8ba2eec936a60dfcb70/data")
                            return blackberry.io.dir.appDirs.app.storage.path;
                        }
                    } else if (util.device.isCordova()) {
                    } else {
                    }
                },

                sharedDocsPath: function () {
                    if (util.device.isBlackBerry()) {
                        if (util.device.isBB10) {
                            return blackberry.io.sharedFolder + "/documents";
                        } else {
                            //Example = "file:///accounts/1000/appdata/YourAppName46cbcbf112f94ccc2f8ba2eec936a.testDev_8ba2eec936a60dfcb70/shared/documents/"
                            return blackberry.io.dir.appDirs.shared.documents.path;
                        }
                    } else if (util.device.isCordova()) {
                    } else {
                    }
                },

                fileCopy: function (fromPath, toPath, newName, callBack) {
                    if (util.device.isBlackBerry()) {
                        if (util.device.isBB10) {

                            // use HTML5 filesystem API
                            var where = null;
                            var handleError = function (err) {
                                util.device.fileError(err, where, callBack);
                                return false;
                            };

                            var copyFile = function (fs, fromPath, toPath, newName) {
                                where = "getFile()";
                                fs.root.getFile(fromPath, {}, function (fileEntry) {
                                    where = "getDirectory()";
                                    fs.root.getDirectory(toPath, {}, function (dirEntry) {
                                        where = "copyTo()";
                                        fileEntry.copyTo(dirEntry, newName, function () {
                                            callBack(true, null);
                                        }, handleError);
                                    }, handleError);
                                }, handleError);
                            };

                            where = "requestFileSystem()";
                            blackberry.io.sandbox = false;
                            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
                            window.requestFileSystem(window.PERSISTENT, 1024 * 1024, function (fs) {
                                copyFile(fs, fromPath, toPath, newName);
                            }, handleError);
                        } else {
                            try {
                                blackberry.io.file.copy(fromPath, toPath); // this is a syncronous call that will throw an exception
                                callBack(true, null);
                            } catch (ex) {
                                callBack(false, "Error: blackberry.io.file.copy()");
                            }
                        }
                    } else if (util.device.isCordova()) {
                    } else {
                    }
                },

                fileDelete: function (path, callBack) {
                    if (util.device.isBlackBerry()) {
                        if (util.device.isBB10) {

                            // use HTML5 filesystem API
                            var where = null;
                            var handleError = function (err) {
                                util.device.fileError(err, where, callBack);
                                return false;
                            };

                            var deleteFile = function (fs, path) {
                                where = "getFile()";
                                fs.root.getFile(path, {}, function (fileEntry) {
                                    where = "remove()";
                                    fileEntry.remove(function () {
                                        callBack(true, null);
                                    }, handleError);
                                }, handleError);
                            };

                            where = "requestFileSystem()";
                            blackberry.io.sandbox = false;
                            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
                            window.requestFileSystem(window.PERSISTENT, 1024 * 1024, function (fs) {
                                deleteFile(fs, path);
                            }, handleError);
                        } else {
                            try {
                                blackberry.io.file.deleteFile(path);
                                callBack(true, null); // above will throw error on fail
                            } catch (ex) {
                                callBack(false, "Error: blackberry.io.file.deleteFile()");
                            }
                        }
                    } else if (util.device.isCordova()) {
                    } else {
                    }
                },

 

Please use plain text.