In many database applications, you need to use a keyword field to find the corresponding data of other fields, and the search speed is fast. For example, in most monitoring programs, after obtaining an Identifier from the terminal, you need to query other data in the data table and display it on the program interface. The general practice is to directly use SQL queries to return query results. In fact, if this identifier is unique and ordered, and the returned results are unique, there is no need to frequently query the database. Data should be first stored in the memory. Of course, it is difficult to store tens of thousands of data rows. The memory storage method I want to talk about is actually the simplest method, that is, the array of elements of the record (structure) type.
The practice is to first define a record type, which is used to save the result data to be displayed on the interface, then define an array, and store all the data from the data table as query and sort into this array. Use the binary method to obtain the corresponding array elements.
The Code is as follows:
Unit u_mydata;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls,
Dialogs, stdctrls, dB;
Type
Pmydata = ^ tmydata;
Tmydata = record
Cardno: integer;
Empno, empname, classname: string;
End;
Tmysearch = Class
Private
{Private Declarations}
List: array of tmydata;
Function search (ibegin, iend, acardno: integer): integer;
Public
{Public declarations}
Destructor destroy; override;
Function clear: Boolean;
Function loadvalue (items: tstrings): Boolean; overload;
Function loadvalue (Dataset: tdataset): Boolean; overload;
Function searchfor (acardno: integer): tmydata; overload;
End;
VaR
Null_data: tmydata;
Implementation
{Tmysearch}
Function tmysearch. Clear: Boolean;
Begin
Result: = false;
Try
Setlength (list, 0 );
Result: = true;
Except
// Can't free the element.
End;
End;
Destructor tmysearch. Destroy;
Begin
Clear;
Inherited;
End;
Function tmysearch. loadvalue (Dataset: tdataset): Boolean;
VaR
I: integer;
Begin
Result: = false;
Try
Clear; // clear the old value first.
With dataset do
Begin
If active then
Begin
Disablecontrols;
Try
Setlength (list, recordcount );
First;
For I: = 0 to recordcount-1 do
Begin
List [I]. cardno: = Fields [0]. asinteger;
List [I]. empno: = Fields [1]. asstring;
List [I]. empname: = Fields [2]. asstring;
List [I]. classname: = Fields [3]. asstring;
Next;
End;
Finally
Enablecontrols;
End;
End;
End;
Except
// Can't load value from dataset.
End;
End;
Function tmysearch. loadvalue (items: tstrings): Boolean;
VaR
I: integer;
Begin
Result: = false;
Try
Clear; // clear the old value first.
Setlength (list, items. Count );
For I: = 0 to items. Count-1 do
Begin
List [I]. cardno: = strtoint (items [I]);
End;
Result: = true;
Except
// Can't load value from strings.
End;
End;
Function tmysearch. Search (ibegin, iend, acardno: integer): integer;
VaR
Mid: integer;
Begin
Result: =-1;
Try
If ibegin <= iend then
Begin
Mid: = (ibegin + iend) Div 2;
If list [Mid]. cardno = acardno then
Result: = mid
Else if list [Mid]. cardno> acardno then
Result: = search (ibegin, mid-1, acardno)
Else
Result: = search (Mid + 1, iend, acardno );
End;
Except
// Can't search the value.
End;
End;
Function tmysearch. searchfor (acardno: integer): tmydata;
VaR
I: integer;
Begin
Result: = null_data;
Try
If length (list)> 0 then
Begin
I: = search (0, length (list)-1, acardno );
If (I> = 0) and (I <length (list) then
Result: = list [I];
End;
Except
// Can't find a value.
End;
End;
End.
{Implemented as a class}
The above code has been tested, but it feels faster than querying the database. In fact, you have to set the same conditions for comparison.