Solved

array from db table

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Component or function to create an ICS-file 2 92
PDF library for Delphi 2 89
Convert Jpg /PNG To GIF 5 103
Create a path if not exists 7 47
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

13 Experts available now in Live!

Get 1:1 Help Now