This is a fairly standard approach. Say your database records are stored in
a struct:
typedef struct {
char name[10];
char address[20];
// etc
} dbrec;
Now to find an entry in the db, assuming its sorted by name, you would do
something like this:
dbrec * searchrec = MemPtrNew(sizeof(dbrec));
StrNCopy(searchrec->name, valFromField, 10);
pos = DmFindSortPosition (dbR, searchrec, compar, 0);
// now need to test pos itself to find out if it is
// > total number of records, or if it does not match
rechandle = DmQueryRecord(dbR, pos);
if (rechandle == 0) return; // no match
recPtr = MemHandleLock(rechandle)
if (compar(recPtr, searchrec, 0) == 0) {
// found actual match at position pos,
}
MemHandleUnlock(rechandle);
MemPtrFree(searchrec);
That's untested code off the top of my head, but the basic idea is sound.
1) allocate a record stucture (or chunk of memory if you use packed database
records) and fill it in with what you want to find
2) call DmFindSortPosition, passing in the pointer to the record you created
in step 1, as well as a pointer to your compare fn.
3) use DmQueryRecord to fetch the returned record found in step 2
4) test to see if you actually got a record
5) if you did, test the record by calling your compare fn with the record
and your search record
6) after you're done, unlock handles and free allocated memory
- John Schettino
Palm OS Programming for Dummies: http://schettino.tripod.com
-----Original Message-----
From: Akshay Shende
To: Palm Developer Forum
Sent: 5/1/00 10:22 PM
Subject: RE: Keeping track of records
I think I've somewhat worked thinks out in relation to records.
Basically I
didn't quite understand the DmFindSortPosition() routine very well!
I still have one doubt though - how to compare something typed into a
field(i.e. not in a record) to the DB records to find a match. One of
the
Palm tutorials suggests that I should build a record in dynamic memory
for
this purpose. In other words: create a record in dynamic memory to hold
the
stuff typed into the field and then compare this record with all the
records
in the DB till a match is found. What I don't understand is:
1. How can I create a 'record' in dynamic memory!!
2. How exactly can I make the DmFindSortPosition/DmComparF
combination compare a dynamic record(whatever that is) to the DB
records?
Thanks for any help. Sorry for the badgering!
Akshay
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Akshay
Shende
Sent: Monday, May 01, 2000 9:49 PM
To: Palm Developer Forum
Subject: RE: Keeping track of records
Thanks a lot for getting back to me Peter!
I think I'm getting closer to understanding records, so let me
run this
last one by you ( and anyone else who can help! ) just to make sure I'm
not
on a wild-goose chase:
So, supposing I have a 5 records database, and have stored a
"Last Name" in
each; this data("Last Names" and the record indices) is stored in a
struct
and loaded into preferences using PrefSetAppPreferences().
In this case, would the following pseudo-code be accuate - my
objective
being to start the application afresh and find some specific person's
"Last
Name":
1. Obtain a CharPtr to the stuff someone has just typed into a
"search"
field.
2. Use DmGetRecord() to run through each of the 5 records,
comparing
the stuff
typed into the "search" field with the stuff stored in the
record. Each
time I call DmGetRecord() I would pass the in appropriate
record index
that has been stored at the time I had created the
records.
3. Get out of the loop when there is a match.
Really appreciate help, and big thanks for bearing with the novices!!
regards,
Akshay Shende
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Peter
Epstein
Sent: Monday, May 01, 2000 6:53 PM
To: Palm Developer Forum
Subject: Re: Keeping track of records
In general, Palm applications should save their state so that when the
user
switches to a different app and then comes back, the state is pretty
much as
it was when they left. The most common way to do this is to store the
necessary state information in the preferences. The tutorial that comes
with
the SDK goes through this, so you can see how it's done. Of course, each
application is different, and you need to figure out a good
representation
for this state which doesn't use too much memory.
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/