August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Basic Database Management Under Palm OS: Managing Records

  • May 30, 2003
  • By Alex Gusev
  • Send Email »
  • More Articles »

In the previous article we have discussed how to enumerate databases installed on Palm device and retrieve their parameters. The next step is to investigate them closer. We will see how to handle data records stored in database and related stuff.

Managing Records

The first thing you need to do is to open the desired database:

DmOpenRef dbP = 
    DmOpenDatabaseByTypeCreator('DATA', 
                                'ALEX', 
                                mode);

This function gets three parameters: database type, creator, and opening mode. Type and creator were discussed in the previous article. They are just a four-bytes integer values to uniquely identify the owner of database. The last parameter defines how to open the database. It may be set to following values:

/******************************************************
 * Mode flags passed to DmOpenDatabase
 *******************************************************/
#define  dmModeReadOnly    0x0001   // read  access
#define  dmModeWrite       0x0002   // write access
#define  dmModeReadWrite   0x0003   // read & write access
#define  dmModeLeaveOpen   0x0004   // leave open when 
                                    //  app quits
#define  dmModeExclusive   0x0008   // don't let anyone 
                                    //  else open it
#define  dmModeShowSecret  0x0010   // force show of 
                                    //  secret records

For our purposes we will use dmModeReadWrite to be able to manage Date Book records.

Opening the Database

The function for opening a database returns reference to open database or NULL if failed. This reference should be used in all of the operations that occur on this database after it is opened. If there was no such database, i.e. DmOpendatabaseBuTypeCreator has failed, then the database will need to be created with the desired flags set. Thus, the standard flow is as follows:

   Err error = 0;
   DmOpenRef dbP;
   UInt cardNo;
   LocalID dbID;
   Word attributes;
   UInt mode = dmModeReadWrite;

   dbP = DmOpenDatabaseByTypeCreator('DATA', 'ALEX', mode);
   if ( dbP == NULL )
   {
      error = DmCreateDatabase (0, "TestDB", 'ALEX', 'DATA', false);
      if ( error )
                   return error;
      
      dbP = DmOpenDatabaseByTypeCreator('DATA', 'ALEX', mode);
      if ( dbP == NULL )
                   return DmGetLastErr();

      // Set the backup bit to get backup copy at PC.
      error = DmOpenDatabaseInfo( dbP, &dbID, 
                                  NULL, NULL, &cardNo, NULL);
      if (error )
      {
               DmCloseDatabase(dbP);
               return error;
      }
      error = DmDatabaseInfo(cardNo, dbID, NULL, 
                             &attributes, NULL, NULL,
                             NULL, NULL, NULL, NULL, NULL, 
                             NULL, NULL);
      if (error )
      {
               DmCloseDatabase(dbP);
               return error;
      }
      attributes |= dmHdrAttrBackup;
      error = DmSetDatabaseInfo(cardNo, dbID, NULL,
                                &attributes, NULL, NULL,
                                NULL, NULL, NULL, NULL, NULL, 
                                NULL, NULL);
      if (error )
      {
               DmCloseDatabase(dbP);
               return error;
      }
   }

After creating the database it's recommended to raise the backup bit for the next sync with a desktop computer. If you need to protect your database from beaming, etc. you may do so here.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel