
Introduction
Welcomed this, my third article on palm development. Now that we have covered the basics and can create an application with some GUI components, the next important thing is to be able to persist data. The palm application philosophy is simple. When an application is started, it should be in the State that it was last left, giving the illusion of a multithreaded system, were in fact only one thread can run at a time. In order for this to be possible, as well as in order for just about any useful application to be written, we need to store data. Because the palm has no external storage mediums, data is stored in memory, in area is known as databases. I am sorry to report that this is somewhat of a misnomer, and does not in any way imply facilities such as SQL or relational tables. Instead what you get is a block of memory which is associated with your creator ID and the name, from which you can select data by either its array style index, or an ID which is guaranteed to be unique in the context of that device, and which would have no relation to the ID returned if the same application were run on a different device.Creating a database
The first step of course is to create a database. The variable which equates to a handle to a database is a DmOpenRef. You should declare one of these as a global in your main header file for each database that you need to keep open. The call to create a database looks like this.Err err = 0;err = DmCreateDatabase(0, "BugSquBugsDYTC", 'DYTC', 'DATA', false);
Opening a database
At this point, we still do not have an open database and we have not used our DmOpenRef. We're now going to correct both of these anomalies. There are two ways to open a database. The first is as follows:DmOpenRef myRef;
DmOpenDatabaseByTypeCreator('DATA' , 'DYTC', dmModeReadWrite);
Err err = 0;LocalID dbID = DmFindDatabase(0,"BugSquBugsDYTC");
gDB = NULL;
if (dbID > 0) gDB = DmOpenDatabase(0, dbID, dmModeReadWrite);
if (!gDB) {err = DmCreateDatabase(0, "BugSquBugsDYTC", 'DYTC', 'DATA', false);
if (err == 0)
{gDB = DmOpenDatabase(0, DmFindDatabase(0,"BugSquBugsDYTC"), dmModeReadWrite);
Creating records
So at this point we have a database on a palm, and a handle to it. Quite obviously the next step is to create some data records with the database, in particular it is likely that our newly created database should have some default values within it. The sample application is called Bug Squasher. It was written as a learning exercise, and in theory is designed to be part of a bug tracking system, where the desktop system updates a list of applications which can have bug reports, and the Palm creates bug reports which can then be uploaded to the main system. The simpler of the two databases simply stores a list of names, which equate to projects for which we can report bugs. The two default values are 'misc' ( for bugs on systems not in the Palm ) and 'BugSqu' ( so we can report bugs on the bug tracking system itself ). The functions we will need to create a record and fill it with data are DmNewRecord, which takes three parameters ( the database to operate on, the index of the record to create, and a length for the record ), and DmStrCopy, which takes a Char * ( derived from our record handle, as we will see ), a start position, and another Char *, which we will copy. Note that I capitalise the word Char, this is a common way for PalmOS to create it's own types, a Char is the type used by Palm, as opposed to char. That they are the same thing is immaterial, Palm create their own types to ensure all types are the same size across platforms. The other functions we will use are MemHandleLock, which returns a void *, and which we call on our new database record, casting the result to a Char *. Finally, MemPtrNew is the Palm equivelant of malloc, and is used to create our string, StrCopy is the equivelant of strcpy and is used to place a value in our string, and MemPtrFree is used to free the string we created. Here is the code:UInt16 index = dmMaxRecordIndex;MemHandle h = DmNewRecord(gDBProj, &index, 5);if (!h)err = DmGetLastErr();
else {Char * s = (Char *) MemHandleLock(h);
Char * pChar = (Char*)MemPtrNew(5 * sizeof(Char*));
StrCopy(pChar, "Misc\0");
DmStrCopy(s, 0, pChar);MemPtrFree( pChar);
MemHandleUnlock(h);
}
index = dmMaxRecordIndex;
h = DmNewRecord(gDBProj, &index, 6);if (!h)err = DmGetLastErr();
else {Char * s = (Char *) MemHandleLock(h);
Char * pChar = (Char*)MemPtrNew(6 * sizeof(Char*));
StrCopy(pChar, "BugSq\0");
DmStrCopy(s, 0, pChar);MemPtrFree( pChar);
MemHandleUnlock(h);
}
Well, that was easy, wasn't it ? But because the databases are just an area of memory and offer no real facilities beyond locking a record in terms of accessing items within a record, what do we do when we want to store more complex data ? The answer is to create a struct that defines our datatype. For example, if we had a strict that looks like this:
typedef struct {
DateType DateEntered;
UInt8 nBugSeverity;
UInt8 nBugType;
} BugRecord;
Char *s;
UInt16 offset = 0;
s = (Char *) MemHandleLock(DBEntry);
DmWrite(s, 0, br, sizeof(BugRecord));
MemHandleUnlock(DBEntry);
typedef struct {
DateType DateEntered;
UInt8 nBugSeverity;
UInt8 nBugType;
char * szBugDesc;} BugRecord;
typedef struct
{DateType DateEntered;
UInt8 nBugSeverity;
UInt8 nBugType;
char szBugDesc[1];
} PackedBugRecord;
void PackBug(BugRecord *br, MemHandle DBEntry){UInt16 length = 0;
Char *s;
UInt16 offset = 0;
length = (UInt16) (sizeof(BugRecord) + StrLen(br->szBugDesc) + 1);
if (MemHandleResize(DBEntry, length) == errNone) {s = (Char *) MemHandleLock(DBEntry);
DmWrite(s, 0, br, sizeof(*br));
DmStrCopy(s, OffsetOf(PackedBugRecord, szBugDesc), br->szBugDesc);MemHandleUnlock(DBEntry);
}
}
Naturally having packed our bug, we also need to unpack it. This is simply a matter of assigning the right value to the variables within the struct. Obviously, if we had more than one string, we would need to use StrLength to figure out the right position for the start of each string in order to assign them. In The code looks like this.
void UnpackBug(BugRecord *bug, const PackedBugRecord *pBug)
{bug->DateEntered = pBug->DateEntered;
bug->nBugSeverity = pBug->nBugSeverity;
bug->nBugType = pBug->nBugType;
bug->szBugDesc = pBug->szBugDesc;
}
Reading back our records
In this example I have used a GUI component which we have not yet discussed, namely a list. The list has been set to be ownerdrawn, and a callback has been set which will be passed the index of the item to draw. The details of this will be covered in a future article. Our strategy is to use the index passed into the overdrawn function to retrieve the appropriate record from our database and render it. This has the advantage of saving us from making a copy of our entire database, saving both space and processor time. There are two ways to search for a record, by index or by unique ID. The unique ID is not as exciting as it sounds, the search is still sequential and therefore has linear growth in complexity. It's far more common to search by index, but if you have the ID, the API is DmFindRecordByID, and it takes a database, a unique ID and a pointer which returns the index of the item. The DmQueryRecord function takes a database and a record number and returns a record handle or NULL to indicate failure. Here is the full code we use in our list callback to read a record.Err err = 0;MemHandle myRecord = DmQueryRecord(gDB, itemNum);
if (!myRecord)err = DmGetLastErr();
else {PackedBugRecord *prec = (PackedBugRecord *) MemHandleLock(myRecord);
BugRecord * rec = (BugRecord*) MemPtrNew(sizeof(BugRecord));UnpackBug(rec, prec);
Deleting Records
Deleting records is easy, just use DmRemoveRecord(gDB, nIndex), where gDB is your handle to a database, and nIndex is the record to delete.Some other stuff
Records have a 64k limit, there is a way to overcome this, but I am not familiar with it. Each record also has a record attributes area, which is one byte in size and contains a deleted bit, a secret bit, a dirty bit, a busy bit and a 4 bit category. Records when they are deleted are not necesarily removed from the Palm. DmRemoveRecord deletes them entirely, DmDeleteRecord deletes the record but keeps the header and sets the bit, and DmArchiveRecord does not delete the record, but sets the bit. The reason for these differences is syncronisation, another future topic, which basically involves moving data between a Palm and a desktop application. Deleted records are moved to the end of the database and are not visible, even if they are still there. They are typically always removed at the next syncronisation.Databases also have their own header, where data like the name, creator, and type is stored, along with some application specific data. This is the place to store data you want to keep only once for the entire database. Additionally, databases can be sorted, through the provided DmInsertionSort and DmQuickSort functions, both of which use a function pointer to allow the programmer to specify the sort order. Insertion while maintaining sort is provided by the DmFindSortPosition API, inserting into an existing record index causes all records below that one to be moved down accordingly.
Although I am unable to comment beyond what the Palm API can tell you about these functions, they seem worthwhile, we have a database at work with 40,000 records and without some sort of binary search, the search times are somewhat untenable.

Make Money Online
|