Appcelerator Titanium has built in support for using SQLite databases either asynchronously or synchronously. It is a fair bit easier doing it synchronously, and for my application, it had to work that way for it to work correctly, so I will go throught that first, and in a later tutorial explain how to set up an asynchronous database.

Using the SQLite database is a simple affair, and makes use of theTitanium.Database namespace and the DB and ResultSet objects.

So, the first step is to connect to a database:

   var db = Titanium.Database.open('session_db');

The parameter is the name of the database to open.

Next, we execute a sql command on the database:

   var rs = db.execute('SELECT [value] FROM [session]
        WHERE [key] = \'' + val + '\'');
   if(rs.isValidRow()){
      val = rs.field(0);
   }
   rs.close();

The result set from the database call is returned, and then we can extract the data out of the resultset. If a command does not return a result set, such as a DELETE or an INSERT command, then the .execute() method can be called without assigning the result to anything. Once the result set has been used, it can be closed.

Once we are done with the database, we can then close it:

   db.close();

More information on the DB object can be found in the Titanium API Documentation.

Share