--- Robert Brinson <[EMAIL PROTECTED]> wrote:
> I've got a small problem. I've not been able to get a
> test application that I have been writing to write to or read
> from a database correctly. I usually end up with garbage. :-)
> Anyway, this basic application consists of one form with two
> text fields and a button.
The most common mistake (which actually has nothing to do with Palm OS
programming but is a C programming issue) is to try to save pointers in
the database. A pointer to a string will not point to anything useful
when you run your application another time. You have to save the
characters that the pointer points to.
> I have read through the first ten chapters of _Palm OS
> Programming: The Developers Guide, 2nd Edition_. The database
> chapter (10) has not been a great help in solving this problem.
Neil shows you how to do everything you mentioned in chapter 10.
> typedef struct
> {
> Char* name; // Hold person's name.
> Char* phoneNum; // Hold their phone number.
> } DemoDB;
> typedef DemoDB* DemoDBPtr;
This structure holds two pointers. Writing this structure to the
database will do you no good. (However, you may want to use this
structure to hold the two pointers while your program is running.)
>
> Err GetFromDatabase(DemoDBPtr& dbPtr, MemHandle& infoH, UInt16 index)
> {
> Err err = 0;
> infoH = DmQueryRecord(dbDemo, index);
> if(!infoH)
> {
> infoH = MemHandleNew(sizeof(DemoDB));
> dbPtr = (DemoDBPtr)MemHandleLock(infoH);
> dbPtr->name = "Robert Brinson";
> dbPtr->phoneNum = "479-524-6631";
> return err; //err = DmGetLastErr();
> }
> else
> dbPtr = (DemoDBPtr)MemHandleLock(infoH);
> return err;
> }
The above code tries to get a handle to a database record. If it
fails, it assigns some pointers to constants in the structure pointed
to by dbPtr. Note: those constants won't be there next time your
program runs, or, if they are still in the same location, it will only
be an accident.
>
> Err AddToDatabase(FieldPtr field1, FieldPtr field2)
> {
> MemHandle a, b;
> DemoDBPtr dbPtr;
> MemPtr infoP = MemPtrNew(sizeof(DemoDB));
> Err error = errNone;
> UInt16 recIndex = 0;
> dbPtr = (DemoDBPtr)infoP;
> a = FldGetTextHandle(field1);
> if(a)
> dbPtr->name = (Char*)MemHandleLock(a);
This gets the handle to field1's text, locks it, then assisns that
pointer to dbPtr->name. Again, that pointer will be useless later on.
etc., etc.
Basically, you need to get a book on C programming and learn about
pointers.
__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/