I realize that saving strings to a DB is a commonly asked question on this
forum. I've read through many posts on the topic and have followed the
instruction to pack strings into the end of the database. Yet, my code
doesn't seem to be doing any saving to the DB at all. I was wonder if
someone could have a look and see what I've done wrong. My procedure is to
create a tempoary record called 'currentAnimalRefRecord '. Then I pack the
contents of that record into the DB. Below are the relavent code snippets.

Thanks,
Tim

/*** Define the packed and unpacked record structures ***/
typedef struct {
 UInt16 alphaRhythm; // Alpha Rhythm in seconds
 UInt16 alphaWindow; // Window around alpha time in milliseconds
 UInt16 warningTime; // Warning time before signal in seconds
 Char* animalName; // Name of the animal
 Char* key2; // Text for schedule key (second key from left)
 Char* key3; // Text for address key (third key from left)
 Char* key5; // Text for toDo key (fifth key from left)
 Char* key6; // Text for memo key (sixth key from left)
} AnimalInfoRecord;

typedef struct {
 UInt16 alphaRhythm; // Alpha Rhythm in seconds
 UInt16 alphaWindow; // Window around alpha time in milliseconds
 UInt16 warningTime; // Warning time before signal in seconds
 Char animalName[1]; // Name of the animal
 Char key2[1]; // Text for schedule key (second key from left)
 Char key3[1]; // Text for address key (third key from left)
 Char key5[1]; // Text for toDo key (fifth key from left)
 Char key6[1]; // Text for memo key (sixth key from left)
} PackedAnimalInfoRecord;


/**** Define some constants used in opening the DB ****/
#define animalRefDBName "animalRef-RBC1"
#define animalRefDBType 'DATA'
#define animalRefDBMode dmModeReadWrite

/**** The part where I open/create the database *****/
LocalID animalRefDB_localID = NULL;
DmOpenRef animalRefDB = NULL; // reference to the animalReference database
AnimalInfoRecord* currentAnimalRefRecord = NULL; // current record
Err errorDB = 0; // (possible) error from creating database

 if (animalRefDB_localID == 0)
 {
  /* The database doesn't exist yet so we create one */
  errorDB = DmCreateDatabase(0, animalRefDBName, appFileCreator,
                                                  animalRefDBType, false);
  if (errorDB)
   return errorDB;

  /* Now we open the newly created database */
  animalRefDB = DmOpenDatabaseByTypeCreator(animalRefDBType, appFileCreator,

animalRefDBMode);

  if (!animalRefDB)
   return DmGetLastErr();
 } else {
  animalRefDB = DmOpenDatabase(0, animalRefDB_localID, animalRefDBMode);
 }


/****** The part where I call the pack function *******/
/*** Note at this point 'currentAnimalRefRecord' has been properly filled
out from field widgets. I've checking it using codewarrior's debugger ***/
index = DmFindSortPosition(animalRefDB, currentAnimalRefRecord, NULL,
   (DmComparF *) AnimalRefDBRecordCompare, COMPARE_BY_NAME);
// Create a new record
newRecordH = DmNewRecord(animalRefDB, &index,
                                           sizeof(PackedAnimalInfoRecord));
if (newRecordH) {
        // Populate the new record with data
        PackAnimalInfoRecord(currentAnimalRefRecord, newRecordH);
}


/****** The pack function *********/
/* This is where things go wrong. Using the Codewarrior debugger, I can
 * see that the contents of 'src_record' are correct.
 * But, nothing ever gets written to 's'. I don't get any errors from the
 * functions as I watch the 'error' variable from within
 * the debugger.
 ***/
Boolean PackAnimalInfoRecord(AnimalInfoRecord *src_record, MemHandle
                                                  animalRefRecord)
{
 UInt32 length = 0;
 Char *s;
 Err error;

 length = (UInt16) (sizeof(src_record->alphaRhythm) +
      sizeof(src_record->alphaWindow) +
      sizeof(src_record->warningTime) +
      StrLen(src_record->animalName) + 1 +
      StrLen(src_record->key2) + 1 +
      StrLen(src_record->key3) + 1 +
      StrLen(src_record->key5) + 1 +
      StrLen(src_record->key6) + 1);

 if (MemHandleResize(animalRefRecord, length) == errNone)
 {
  s = (Char *) MemHandleLock(animalRefRecord);

  /*** 's' remains blank throughout the following statements. What am I
doing wrong? ***/
  error = DmWrite(s, OffsetOf(PackedAnimalInfoRecord, alphaRhythm),
                     &src_record->alphaRhythm,
sizeof(src_record->alphaRhythm));
  error = DmWrite(s, OffsetOf(PackedAnimalInfoRecord, alphaWindow),
                     &src_record->alphaWindow,
sizeof(src_record->alphaWindow));
  error = DmWrite(s, OffsetOf(PackedAnimalInfoRecord, warningTime),
                     &src_record->warningTime,
sizeof(src_record->warningTime));

  error = DmStrCopy(s, OffsetOf(PackedAnimalInfoRecord, animalName),
                     src_record->animalName);
  error = DmStrCopy(s, OffsetOf(PackedAnimalInfoRecord, key2),
                     src_record->key2);
  error = DmStrCopy(s, OffsetOf(PackedAnimalInfoRecord, key3),
                     src_record->key3);
  error = DmStrCopy(s, OffsetOf(PackedAnimalInfoRecord, key5),
                     src_record->key5);
  error = DmStrCopy(s, OffsetOf(PackedAnimalInfoRecord, key6),
                     src_record->key6);

  MemHandleUnlock(animalRefRecord);

  return true;
 } else {
  return false;
 }
}




-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to