Solved

array from db table

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 79
Error E2158 compiling with Delphi XE10 Seattle 2 112
How to build JSON File in Delphi 6 3 45
Firemonkey DbLookupComboBox equivalent ? 2 55
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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