(If you wish to use TQDBView's easier, higher-level approach you can skip to that section.)
The example projects (address, animals, and archive) show how to access QDB files directly via a TQDB component. TQDB treats each item in the file as an unstructured stream of bytes. It is up to the developer to parse the stream into meaningful values.
As such TQDB has no concept of fields or data types. If an item corresponds to a fixed-length data structure (e.g., a Pascal record) it is easily read from the item stream in one step, e.g.:
type
TX = record
a: integer;
b: extended;
c: array [0..4] of char;
end;
...
m:=TMemoryStream.Create;
try
MyQDB.ExactMatch('aardvark');
MyQDB.Get(m);
// read the record in one go
m.Read(MyX,SizeOf(TX));
finally
m.Free;
end;
If the item contains sub-items of variable length the en/decoding must be more sophisticated. Any scheme can be used depending upon the developers needs, preferences, and ingenuity. One of the simplest approaches is to prefix each variable-length sub-item with an 32-bit integer code representing the length of the coming block of bytes.
type
TX = record
a: integer;
b: extended;
c: string;
end;
...
m:=TMemoryStream.Create;
try
MyQDB.ExactMatch('aardvark');
MyQDB.Get(m);
// read the record field by field
m.Read(MyX.a,SizeOf(TX.a));
m.Read(MyX.b,SizeOf(TX.b));
m.Read(len,SizeOf(len));
SetLength(MyX.c,len);
m.Read(MyX.c[1],len);
finally
m.Free;
end;
The TQDBItem and TQDBView components store each field prefixed with a 32-bit length code.