Re: Delphi Array and MS/Access DB to store and retrieve data
Posted by webmaster Guido on March 16, 2004 at 18:30:01:
In Reply to: Using a Delphi Array and MS/Access DB to store and retrieve data posted by Lionel Joyner P12219 on March 14, 2004 at 19:16:18:
: I have search the WWW trying to find an example that would fit what I am trying to do but I may be headed in the wrong direction and need an example to work with to set my direction.
: An example (array with two legs in a repeat loop plus leg results storage in a DB) would be most helpful.
If I understand you well, you want to calculate and store the coordinates of the end-points of two "legs", given:
As you noted, calculating goes like this, to be repeated for each leg:
If you store the given parameters and the resulting coordinates in an static array of records, that's the easiest.
type TLegPair = record Dist1, Dist2, Angle1, Angle2, X1, Y1, X2, Y2: real; end;
We must declare how many "LegPairs":
const NrOfLegPairs = 100;
Here's the array definition, using a static array:
var LegPairs: array [1..NrOfLegPairs] of TLegPair;
The main loop, to input the data and store them:
procedure DoTheLoop; var i: integer; // loop counter begin for i := 1 to NrOfLegPairs do begin with LegPairs[i] do begin // Get 2 distances and 2 angles from somewhere Dist1 := ...; Angle1 := ...; Dist2 := ...; Angle2 := ...; // Calculate coordinates X1 := Dist1 * Sin(Angle1); Y1 := Dist1 * Cos(Angle1); X2 := Dist2 * Sin(Angle2); Y2 := Dist2 * Cos(Angle2); end; end; end;
Let's say that instead you want to save to a table T1 with eight fields of the type "real" (floating point number), named "Dist1", "Dist2", and so on. The main loop becomes:
procedure DoTheLoop; var i: integer; // loop counter begin for i := 1 to NrOfLegPairs do begin T1.Append; // create empty record T1Dist1.Value := ...; T1Angle1.Value := ...; T1Dist2.Value := ...; T1Angle2.Value := ...; // Calculate coordinates T1X1.Value := T1Dist1.Value * Sin(T1Angle1.Value); T1Y1.Value := T1Dist1.Value * Cos(T1Angle1.Value); T1X2.Value := T1Dist2.Value * Sin(T1Angle2.Value); T1Y2.Value := T1Dist2.Value * Cos(T1Angle2.Value); T1.Post; // save the record to disk end; end;
Before constructing the loop, we must know:
- Is there a fixed number of "LegPairs"? If not, we can't use a static array to store the results, but we must use a structure with a dynamic size, such as: a dynamic array, a database on disk, a textfile, or a file-of-records. Of course, then also the structure of the loop changes, like this:
while not ItsDone do begin // Calculate records and store: ... end;
"ItsDone" can be a function that returns True or False. E.g. return False if no more data available, or if the user clicked a "Stop" button, whatever...
- How are the parameters Dist1, Dist2, Angle1, Angle2 obtained?
So if you give some more details, we could try to complete the code.
And forgive my curiosity ;-) why do you want to store these results in a database, for later recall? Why not you calculate them at the moment you need them, instead of reading the stored results from a slow database on disk?
Re: Using a Delphi Array and MS/Access DB to store and retrieve
data lionel 3/17/2004
[ DelphiLand: free Delphi source code, tips, tutorials ]