Now that you know how to set up databases, you need to populate them with records. How you sort and therefore find a record is usually determined when you create it. Let’s first look at the mechanics of finding a record. After that, we’ll create a new record.
If your records are sorted based on the value of a field (or fields) within the record, you can do a binary search to find a particular record. If your records aren’t sorted (or you are looking for a record based on the value of an unsorted field), you need to iterate through all the records, testing each record to see whether it is the one you want. Section 6.3.6 later in this chapter shows how to iterate through all records. If you are looking for a unique ID, there’s a call to find a record.
If you’ve got the unique ID, you get the record number using DmFindRecordByID :
UInt recordNumber; err = DmFindRecordByID(gDB, uniqueID, &recordNumber);
Note that this search starts at the first record and keeps looking until it finds the one with a matching unique ID.
If you have records sorted by some criterion (see Section 6.3.7 later in this chapter), you can do a binary search to find a specific record. First, you need to define a comparison routine that compares two records and determines the ordering between the two. Here are the possible orderings:
The first is greater than the second
The second is greater than the first