DynaWorks Tutorial

How to write applications with the DynaWorks framework


As you see, you build up your record by adding named "fields" of a certain type. Each field type is derived from the class RecordField. At the moment the following field types are available:

DynaDb: Table Of Content

DynaDb - creating database-aware applications easily

The DynaWorks framework now includes DynaDb, a set of classes to be used for database operations. DynaDb not only helps you to use your custom databases, it also supports you in accessing (limited to read access for the moment) the Palm build-in databases (address, datetime, notes ...) as well.

The DynaDb classes are ment to simplify the task of working with different databases. Instead of writing specific database methods for every database, you can use DynaDb abstract layer to handle your record layouts on the fly.

Back to top of page ...

Defining your own recordset

Databases (at least on the palm) are a ordered collection of variable-length datastructures. Normally you devide each record (entry in the database) into fields; the sequence of "fields" in a record is fixed, but the fields can have (in case of Strings do have) variable length.

You start by deriving from class Record to define your own record structure, that is the sequence (and eventually size) of each field:

public class MyRecord extends Record {

   public MyRecord () {

      // add fields to record.
      addField ("name", new StringField ());
      addField ("url", new StringField ());
      addField ("inetaddr", new BytesField (4));
      addField ("port", new ShortField ());
      addField ("login", new StringField ());

      // allocate the object
      alloc ();
   }
}

BytesField
a byte array of fixed length
StringField
a String of variable length
ShortField
a short value (2 Bytes)
IntegerField
an integer value (4 Bytes)

Back to top of page ...

Opening the database

First you have to create a Palm database object. The constructor of the PalmDB class requests a string arguments, that identifies the name of the database, the creator id and the type id:

// create a database object
PalmDB db = new PalmDB ("MYAP:DATA:MyAppDatabase");
This database object can then be opened for read/write by

// forced open on database
if (!db.open()) { ...
The database will be automatically created if it does not exist yet; only if a database could not be created nor opened, the call will fail.

Back to top of page ...

Reading and writing records

You can simply write a record to the database:

// build internet address.
bytes[] addr = new byte [4];
addr[0] = 192; addr[1] = 168; addr[2] = 23; addr[3] = 5;

// setup new record.
MyRecord rec = new MyRecord ();
rec.setString ("name", "Smith");
rec.setString ("url", "http://www.smith.com");
rec.setBytes  ("inetaddr", addr);
rec.setShort  ("port", "80");
rec.setString ("login", "system/manager");

// write new record to database.
if (!db.addRecord (rec)) { ...

   /////////////////////////////////////////////////////////////////
   // read record from database at position 'idx'
   Record rec = new Record ();
   if (db.getRecord (idx, rec)) {

      // read attributes
      String name = rec.getString ("name");
      String url = rec.getString ("url");
      byte[] addr = rec.getBytes  ("inetaddr");
      short port = rec.getShort  ("port");
      String login = rec.getString ("login");
   }

Back to top of page ...

Closing the database

To close a database simply call the 'close()' method of the DynaDB you want to close.

Back to top of page ...


Copyright © 2000, Bernd R. Fix. All Rights Reserved.