Solved

array from db table

Posted on 2000-03-29
5
131 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
problem when i try to pack my dll file with upx 9 71
How to fill array with TArray.Create? 14 78
Magic Software info 18 125
When i run adoquery my application freezes 26 150
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now