How To Store Data in your BlackBerry Widget

How-to

BlackBerry® Widgets offer great means of storing data offline in your application. You can either access the SQLite database or store data on an SD card through your Widget.

Before going into how this is done, let’s take a quick look at what SQLite is. With its reputation for reliability and that it can be used across virtually any platform, SQLite can be used as the onboard backend data store. SQLite is an open source project, and requires only a built in eMMC card (found on the BlackBerry® Bold™ smarpthone/BlackBerry® Storm™ smartphone currently) or an SD Card to be inserted into the device. Data types that are used in SQLite are INTEGER, REAL, TEXT, BLOB and NULL.

In order to access SQLite and harness Google Gears™, you’ll only need to include the following code to initialize the Gears API:

// Get the Gears Factory
if (typeof GearsFactory != 'undefined') {
factory = new GearsFactory();
}

Once the Gears object has been initialized, you can open the SQLite database and begin your SQL transactions:

//Global handle to the database
var _db;
function openDatabase() {
var db = google.gears.factory.create('beta.database');
db.open('itemSet');
db.execute('create table if not exists Items (Phrase text unique)');
_db = db;
}

When using the SQLite database, you are able to run through a variety of commands that you’d normally be able to use on a SQL database. Further documentation on the exact commands can be found on the SQLite website.

Now that we’ve gone over one of the means of storing data, lets shift gears to move towards SD cards. What is an SD card? It is a memory card developed for use in portable devices, that use either FAT or FAT32 file systems (which allow for use on virtually any host device with a reader).

In Widgets, the blackberry.io.file namespace is used to access files on the SD card. Some of the more commonly used methods are:

  • blackberry.io.file.saveFile
  • blackberry.io.file.copy
  • blackberry.io.file.deleteFile
  • blackberry.io.file.exists
  • blackberry.io.file.open
  • blackberry.io.file.readFile

Here is a quick example of how to save and read a file from the SD card:

function saveFile() {
var textarea = document.getElementById("taContents");
if (blackberry.io.file.exists(filePath)) {
if (check == true) {
//if the file exists, you have to delete the existing one before saving the new copy
blackberry.io.file.deleteFile(filePath);
}
}
var blob = blackberry.utils.stringToBlob(textarea.value);
blackberry.io.file.saveFile(filePath, blob);
}

function readFileComplete(filePath, data) {
var fileContents = blackberry.utils.blobToString(data);
}

I think these two methods of storing data will prove to be very beneficial to developers out there, as I’ve often received questions about how to architect a solution around a web app to have persistent data on the BlackBerry® smartphone. I welcome any comments and questions regarding either of these methods of storing data, and would also love to hear about any other methods you’ve used to work around data storage!

SQLite is a trademark of Hipp, Wyrick & Company, Inc. Google and Gears are trademarks of Google Inc. All other trademarks are the property of their respective owners.

About Tim W.

Tim works on the Developer Relations team at BlackBerry, focusing on WebWorks, HTML5, and Open Source.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus