Error alert! Because this is copyrighted software, I changed some of the
variable names before posting it and in doing so, inadvertently swapped
the source and dest in the DmWrite statement. The line should be:

err = DmWrite (LockedCatchRecord, 0, ThisCatchRec, CatchRecLen);

Also, in looking at this old routine, I realize you could trade some
overhead for bytes by using DmStrCopy's directly into the new record.

  > -----Original Message-----
  > From: [EMAIL PROTECTED]
[mailto:bounce-palm-
  > [EMAIL PROTECTED] On Behalf Of Randall Pulsifer
  > Sent: Friday, August 22, 2003 10:28 PM
  > To: Palm Developer Forum
  > Subject: RE: Strings and databases
  > 
  > Brad's suggestion will get you working, but when you have just one
  > variable string, you can make future packing easy by putting this
field
  > at the end in your definition of the struct type. The code below is
from
  > one of my projects where I know the size of the first two arrays are
  > always 6 & 4, but the last may be 3 chars or 50...
  > 
  > 
  > 
  > MemHandle           ThisCatchRecHandle;
  > typedef struct
  >     {
  >     Char Lbs[6];
  >     Char ID[4];
  >     Char Species[1]; // defining it this way keeps the compiler off
  > your back when the last item is of varying length
  >     }CatchRecord;
  > 
  > CatchRecord *       ThisCatchRec, * LockedCatchRecord;
  > 
  > switch (ThisEType)
  >     {
  >             case winEnterEvent:
  >                     frmP = FrmGetActiveForm();
  >                     FrmSetFocus (frmP,FrmGetObjectIndex
  > (frmP,fldEnterLbs));
  >                     break;
  > 
  >             case ctlSelectEvent:
  >                     if (eventP->data.ctlSelect.controlID == LbsOK)
  > // tapped the OK button
  >                             {
  >                             UInt32  StrLen,CatchRecLen;
  >                             UInt16  atPos = 0;
  >                             MemHandle  RecHandle;
  >                             // get numeric string from field
  >                             strLbs =
  > FldGetTextPtr(GetObjectPtr(fldEnterLbs));
  >                             StrXLen = StrLen(gSelectedStr); // don't
  > know how long this one is
  >                             CatchRecLen = StrXLen + 10; // 10 is the
  > sum of .Lbs and .ID's memory
  >                             ThisCatchRecHandle =
  > MemHandleNew(CatchRecLen);
  >                             ThisCatchRec =
  > MemHandleLock(ThisCatchRecHandle);
  >                             // create byte record
  >                             RecHandle = DmNewRecord(gCatchDBRef,
  > &atPos, CatchRecLen);
  >                             LockedCatchRecord =
  > MemHandleLock(RecHandle);
  > 
  >                             StrCopy(ThisCatchRec->Lbs,strLbs);
  >                             StrCopy(ThisCatchRec->ID,gSelectedID);
  > 
  > StrCopy(ThisCatchRec->Species,gSelectedStr);
  > /*
  > Parameters <-> recordP Pointer to locked data record (chunk
pointer).
  >             -> offset Offset within record to start writing.
  >             -> srcP Pointer to data to copy into record.
  >             -> bytes Number of bytes to write.*/
  >                             // write to gCatchDBRef
  >                     err = DmWrite (ThisCatchRec,
  > 0,LockedCatchRecord, CatchRecLen);
  >                             err =  DmReleaseRecord (gCatchDBRef
  > ,atPos,true);
  >                             MemHandleUnlock(RecHandle);
  >                             MemHandleUnlock(ThisCatchRecHandle);
  >                             MemHandleFree(ThisCatchRecHandle);
  >                             gSelected = false;
  > 
  >                             }
  >                             break;
  > 
  >   > -----Original Message-----
  >   > From: [EMAIL PROTECTED]
  > [mailto:bounce-palm-
  >   > [EMAIL PROTECTED] On Behalf Of Yogesh Saletore
  >   > Sent: Friday, August 22, 2003 5:09 PM
  >   > To: Palm Developer Forum
  >   > Subject: Strings and databases
  >   >
  >   > Hello,
  >   > I'm fairly new at C for Palm and Palm programming. I need some
help
  > with
  >   > recording strings in databases. Right now I just want it to
work,
  > and
  >   > don't
  >   > really want to deal with any packing until later.
  >   >
  >   > I have an error message I want to store, and it is part of a
larger
  >   > struct:
  >   >     typedef struct
  >   >     {
  >   >      char *note;
  >   >      enum ErrorLevel level;
  >   >      Boolean read;
  >   >     } ErrNote;
  >   >
  >   > As you can see, note is a string pointer. Now I have all of my
error
  >   > messages as string literals: "lasdkf". I copy them one by one,
  > depending
  >   > upon which are needed, into a tempory string array:
noteText[300].
  > My
  >   > problem is getting the data from noteText into ErrNote
note.note. In
  >   > order
  >   > to use StrCopy, I must reference the *note pointer to some
memory
  > block.
  >   > The
  >   > initial copying appears to work fine. I then copy ErrNote note
to
  > the
  >   > database using DmWrite, and then the string as well, using
  > DmStrCopy.
  >   > The
  >   > error results when I attempt to read back that same string from
the
  >   > database. It seems that it wants to use the same memory block I
had
  >   > first
  >   > referenced it to, which now no longer exists. Therefore, I
receive
  > many
  >   > memory errors of it attempting to access memory incorrectly. If
I
  > leave
  >   > the
  >   > memory block as is, locked and don't free it, it works fine, but
I
  > then
  >   > have
  >   > a memory leak.
  >   >
  >   > Can anyone please help me find a way to copy the contents of
  > noteText
  >   > into
  >   > ErrNote note.note?
  >   >
  >   > Thanks,
  >   > Yogesh Saletore
  >   >
  >   >
  >   >
  >   > --
  >   > For information on using the Palm Developer Forums, or to
  > unsubscribe,
  >   > please see http://www.palmos.com/dev/support/forums/
  > 
  > 
  > --
  > For information on using the Palm Developer Forums, or to
unsubscribe,
  > please see http://www.palmos.com/dev/support/forums/


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

Reply via email to