How To Store Data in your BlackBerry Widget


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');'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 namespace is used to access files on the SD card. Some of the more commonly used methods are:


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 ( {
if (check == true) {
//if the file exists, you have to delete the existing one before saving the new copy;
var blob = blackberry.utils.stringToBlob(textarea.value);, 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.

Join the conversation

Show comments Hide comments
+ -
  • Sumit

    Hey Tim,

    What do you have to say about Persistent Objects? Google gears can work on phones with external memory cards since it requires posix file system as I read in another article. So it won’t work on all BB mobile devices.
    There was a post on Persistent Objects being phased out in future BB development. Can you throw some light on this.


  • Jayesh Italiya

    This is working fine but how to compare with string??

blog comments powered by Disqus