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
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
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)
- 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
- Comment the line initializing the database. So, comment the line:
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
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
Application Screen Shots
My Experience
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.
Application Screen Shots
My Experience
- 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:
Thanks for sharing the information
ReplyDeleteFor more info : Oracle Certification Path