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/Bookmark

Related posts:

  1. Titanium Tutorial: How to upload a file to a server During the last several weeks I have been making considerable...
  2. Titanium Tutorial: Accessing files on the local system Out of all the features included in Appcelerator Titanium which...
  3. Titanium Tutorial: Using the file selection dialog boxes Appcelerator Titanium enables you to show a dialog box to...
  4. Titanium Tutorial: Window management Appcelerator Titanium has a rich set of Window management methods...
  5. Titanium Tutorial: Setting up a menu In Appcelerator Titanium, setting up a menu is very easy....

Related posts brought to you by Yet Another Related Posts Plugin.