May 12, 2011

Integrating Android App Inventor apps with DB2

In the end of my last post, Easy Android Development with App Inventor, I was wondering about App Inventor integration with external data from different databases. How about using the TinyWebDB component for that? IBM recently released a new version of DB2 Express-C, Fix Pack 4, so I think it would be a good idea to try it out!


DB2 Express-C is the free version of DB2, IBM's database server. It's free for development, production and even distribution. The new features introduced in this version, as well as the ones introduced in previous Fix Packs, are described in What's new with DB2 Express-C 9.7.4, Raul Chong's article on developerWorks.

If you run Ubuntu and feel like trying it out, I recommend reading Installing and Testing DB2 9.7.4 on Ubuntu 11.04, where you can find tips on going through the installation process.


As I described in the last post, both TinyDB and TinyWebDB use a simple key-value based database concept. There are no different tables, extra columns, functions or queries. Given the key, the API allows the user to store and retrieve values. When storing a value, if the key doesn't exist, a new entry is created. If it does exist, the old value is overwritten. Simple like that!

That way, it's impossible to just connect App Inventor with a DB2 database, there has to be an interface for that. An easy-to-implement alternative would be a simple Sinatra app! Sinatra is a "DSL for quickly creating web applications in Ruby with minimal effort". It allows developers to quickly write Web services, which are very useful for an App Inventor/DB2 interface, once TinyWebDB component sends REST requests to a server, that can be specified, and gets JSON objects back with the appropriate content.


Here's my idea: put up a DB2 instance, create a Sinatra server mapping Web services to DB2 operations and adding a TinyWebDB component to an App Inventor application, setting the service URL to point to the Sinatra server. Sounds complicated? Well, it isn't, keep reading! :)
  1. Setup DB2

    Get to your DB2 instance and create a new database, called APPINV. In this database, create a table DATA with two String columns, KEY and VALUE. To do that, launch a CLP and issue the following commands:

  2. Setup the Sinatra server

    First you need to install some gems, as you can see in the first lines of the code below. They should be pretty straight-forward to get. You may have issues with the DB2 driver gem, ibm_db. If you do, check the tutorial Installing Ruby on Rails and DB2 on Ubuntu 11.04, it's definitely going to help you out.

    After that, you'll have to type the correct password in the the IBM_DB.connect command and launch the server:

    This should launch a new server listening to, which is where you should point your App Inventor app to!

  3. App Inventor application

    Finally, go to App Inventor! Get one of your existing applications, or create a new one, and launch the Designer interface. Once there, you can find the TinyWebDB component (still) under the 'Not ready for prime time' section, on the left panel. Drag it to you app, select it and, in the Properties column, set theserviceURL to

    App Inventor: TinyWebDB configuration

After following these 3 steps, you should have your TinyWebDB working with your DB2 instance! This is a really simple example, but it's definitely a good way to integrate your app with existing DB2 databases. What you have to do next is customize the Sinatra server to interact specifically with your data.

That's it! If you have issues playing around with this tutorial, write a comment, I can help with it and give more details.

No comments:

Post a Comment