On Thu, 30 Nov 2006, lulu wrote:
> Do you know if I need to convert data to Hex to be able to write to a
> Database?
>
> I'm trying to save a record to a database and then retrieve, but when I load
> the application again and I try to retrieve the data, I only have an empty
> record.
>
> This is my code to write when I quit the application. I'm using PODS
> static void AppStop(void)
> {
> FormType* pForm;
> MemHandle RecHandle;
> MemPtr RecPointer;
> FieldType* field_text;
>
>
>
> pForm = FrmGetActiveForm();
>
> field_text = FrmGetObjectPtr(pForm, FrmGetObjectIndex(pForm, MainNameField));
This does not do what you think it does. This gets a pointer to the field
object, which is not the text in the field. To get to the text in the
field, you need to add a statement like:
char *text = FldGetTextPtr(field_text);
And write that variable in your DmWrite call, below.
> RecHandle=DmQueryRecord(ntDB, index);
I'm not sure what this statement is supposed to be doing. If you think
it's supposed to be initializing 'index', it isn't.
> RecHandle=DmNewRecord(ntDB, &index, FldGetTextLength(field_text));
Before making this call, you should initialize index to some value. Also,
instead of using FldGetTextLength(), you should either use
FldGetTextLength(field_text)+1, or StrLen(text)+1. The +1 is necessary to
hold the trailing null character.
> RecPointer = MemHandleLock(RecHandle);
> DmWrite(RecPointer, index , &field_text, FldGetTextLength(field_text));
You shouldn't use index here. 'index' is the number of this record in the
database. The parameter to DmWrite that you've put 'index' into is the
offset into 'RecPointer' where writing should start. You probably want to
use 0 (zero) for this parameter. Also, you need to use either
FldGetTextLength(field_text)+1 or StrLen(text)+1 for the 4th parameter, so
that the terminating null character gets written. Having the terminating
null character be written makes it easy to treat the data as a string when
you read it from the database.
> MemPtrUnlock(RecPointer);
You can use MemPtrUnlock() here, but MemHandleUnlock(RecHandle) may be a
little cleaner, as you'd be pairing it with a MemHandleLock() call.
> DmReleaseRecord(ntDB, index, true);
> DmCloseDatabase(ntDB);
> FrmCloseAllForms();
> }
>
> And this is my code when I start the App
>
> static Err AppStart(void)
> {
> MemHandle RecHandle;
> MemPtr RecPointer;
> Char *s;
> Err error;
>
> ntDB = DmOpenDatabaseByTypeCreator(DBType, CreatorID, dmModeReadWrite);
> if (!ntDB)
> {
You don't close this brace anywhere.
> error=DmCreateDatabase(0, DBName, CreatorID, DBType, false);
> if(error==0)
> ntDB = DmOpenDatabaseByTypeCreator(DBType, CreatorID,
> dmModeReadWrite);
>
This is probably where the above brace should be closed, since you
probably want to read from the database if it exists. If it didn't exist,
you probably don't want to try reading from it.
> RecHandle=DmQueryRecord(ntDB, index);
You need to set 'index' to some value before making this call.
>
> RecPointer = MemHandleLock(RecHandle);
Now that you've locked the pointer, do you want to get a value out of it?
> MemPtrUnlock(RecPointer);
See comments above about pairing MemHandleLock with MemHandleUnlock. This
one is optional.
>
> FrmGotoForm(MainForm);
> return errNone;
> }
>
> --
> For information on using the PalmSource Developer Forums, or to unsubscribe,
> please see http://www.palmos.com/dev/support/forums/
>
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/