Solved

array from db table

Posted on 2000-03-29
5
132 Views
Last Modified: 2010-04-04
Can someone please give me a code example of getting data from a table and putting it into a two dimensional array.

Thank You.
0
Comment
Question by:fiinfo
  • 3
5 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 2668193
Sample bellow is applicable in case your know maximum fields and records of a table. In other case use dynamic arrays instead static.
-------
const MaxRecNo = 100;
        MaxFldNo=20;

var A : array[0..MaxRecNo-1,0..MaxFldNo-1] of variant;

procedure ....
var I,N : integer;
begin
    Table1.Open;
    N:=0;
    while not Table1.EOF do
    begin
       for I:=0 to Table1.Fields.Count-1 do A[N,I]:=Fable1.Fields[I].AsVariant;
       inc(N);
       Table1.Next;
    end;
   Table1.Close;
end;
--------

Cheers,
Igor.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2668213
Lets assume we have a table with fields "X", "Y",and "Value", integer, integer and double respectively, then you could do this;

var
  TheArray = Array[1..MaxX, 1..MaxY] of Double;

Table.First;
while not Table.eof do
  begin
    TheArray[Table.FieldByName('X').AsInteger,
             Table.FieldByName('Y').AsInteger] := Table.FieldByName('Value').AsFloat; // or AsDouble...
    Table.Next;
  end;

Cheers,

Raymond.


0
 

Author Comment

by:fiinfo
ID: 2671274
I'm sorry, I don't understand your example (my fault not yours I'm sure).
My solution:

procedure....
var
  myArray : array of array of variant;
  F, R, n, i: Integer;
begin
  table1.open;
  table1.first;
  R := table1.recordcount;
  F := table1.fieldcount;
  setLength(myArray, R, F);
  while not Table1.EOF do
    begin
        for n := 0 to Table1.RecordCount-1 do
        begin
            for i:= 0 to Table1.FieldCount-1 do
            begin
                myArray[n, i]:= Table1.fields[i].AsVariant;
                listbox1.items.add(myArray[n,i]);//just to look at the result
            end;
            table1.next;
        end;
    end;
table1.close;
end;

If you can see a problem with this solution, I'd appreciate your input.
Thank YOu.

0
 
LVL 9

Expert Comment

by:ITugay
ID: 2672486
Rob has shown you the way how to store and retrieve two-dimension array of float values into/from table.

Now about YOUR solution. It's look like my sample but only with dynamic array. And it's seems like you have it. You looking for what? For bugs? For another way?

Igor.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2672488
Not Rob, sorry Raymond.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question