Tuesday 21 August 2012

Example Step5:Demo Application

Now that we have installed DMS and set up the mobile sync client along with couple of tables, we can now look at a simple Titanium application.

Note: The sample application that I use is adapted from a Titanium database example shared by Hammer! In his/her example, Hammer creates the two database tables CATEGORIES and ITEMS using the FireFox add-on SQLite Manager and includes it as a file in the Titanium project. Since the tables are already available on the device in our case, we will ignore this step. I also have made a couple of changes to the sources. Instead of using the method Titanium.Database.install, we will use the method Titanium.Database.open since the database is already available on External Storage (SD Card) and is not in the ApplicationDataDirectory in internal memory

Step 1: Create a New Titanium Project
  • Create a new Mobile project in Titanium. I named my project DB_Test and set the Application ID to net.adkoli.dbtest (this must relate to what you set for DATA_DIRECTORY in OSE.INI; please pay extra attention to this)
Step 2: Replace application files
  • Use instructions in Step 4 of Hammer's sample to replace app.js and few other files of the new Titanium project created in Step 1 above.
  • Click File->Refresh
Step 3: Edit app.js for our needs
  • Comment the line initializing the database. So, comment the line:
Titanium.Database.install('content.sqlite','contentDB');

This is because we will be using the Ti.Database.open method to open the database stored in external storage 

Step 4: Edit tab_categories.js
  • There are two database execute calls in this file. The first one is to fetch records from the CATEGORIES table and the second one is to fetch records from the ITEMS table.
  • In the section "populate category array from database", add a statement to open the database that we have installed on the device. Comment the line 
 var db = Titanium.Database.open('contentDB');

 in the existing tab_categories.js file provided by Hammer.

Replace it with the following:

if (Ti.Platform.name === 'android' && Ti.Filesystem.isExternalStoragePresent()) {
     var db = Titanium.Database.open('/sdcard/net.adkoli.dbtest/AA/TEST.db');
    }

Be sure to modify the path and database name to match the location of your database.

Make the same change in the function(showItemsInCategory) in this file.

Step 5: Build the Application and Install

  • Build the application and run in the Emulator to ensure that there are no errors
  • Then connect the Android device via the USB port and use the option "Install to Android Device"
  • Pay close attention to the log window. If all goes well you should see a confirmation that application was installed on device and that you can launch it
  • Launch DB_Test or whatever you named your app and you should see the sample application with data.
You can download a copy of my app.js and tab_categories.js files

Application Screen Shots

My Experience

  1. I have hardcoded the path to the database in the Ti.Database.open method because I could not get my code with system variables and calls to Ti.Filesystem.externalStorageDirectory working. Ideally, we should be using something like:
var db = Ti.Database.open(Ti.Filesystem.externalStorageDirectory + 'AA' + Ti.Filesystem.separator + 'TEST.db'). I kept getting an error "contains a path separator". If someone gets this going, please share your comments


1 comment:

Note: only a member of this blog may post a comment.