我的一张数据表是这样的:
表名:info
字段名:no,no2
no    no2
 1     0
 1     1
 1     2
 1     3
 2     1
...   ...通过sql,把no所对应的数据取出并存入到一维数组中
例如:no=1 时,对应的no2值为:0,1,2,3,怎样把0,1,2,3存入到一维数组中
我的代码是这样的:
var
  i,j,count:integer;
  arr:array[0..3] of integer;
begin
with ADOQuery1 do
begin
sql.Clear;
sql.Add('select count(*) as count from info where no=''1''');
open;
count:=fieldbyname('count').AsInteger;sql.Clear;
sql.Add('select no2 from info where no=''1''');
open;
for i:=1 to count do
begin
  arr[i]:=fieldbyname('no2').AsInteger ;//这里在运行时会出错
end;

解决方案 »

  1.   

    for i:=0 to count-1 do 
    begin 
      arr[i]:=fieldbyname('no2').AsInteger ;//下标是0,所以出错了
    end; 
      

  2.   

    arr是4个元素的数组,而数据库里no=1的记录多过4个就会出错。var 
      i,j,count:integer; 
      arr:array of integer; 
    begin 
    with ADOQuery1 do 
    begin 
    sql.Clear; 
    sql.Add('select count(*) as count from info where no=''1'''); 
    open; 
    count:=fieldbyname('count').AsInteger; 
    setlength(arr,count);
    sql.Clear; 
    sql.Add('select no2 from info where no=''1'''); 
    open; 
    for i:=0 to count-1 do 
    begin 
      arr[i]:=fieldbyname('no2').AsInteger ;//这里在运行时会出错 
    end; 
      

  3.   

    的一张数据表是这样的: 
    表名:info 
    字段名:no,no2 
    no    no2 
    1    0 
    1    1 
    1    2 
    1    3 
    2    1 
    ...  ... 通过sql,把no所对应的数据取出并存入到一维数组中 
    例如:no=1 时,对应的no2值为:0,1,2,3,怎样把0,1,2,3存入到一维数组中 
    我的代码是这样的: 
    var 
      i,j,count:integer; 
      arr:array[1..4] of integer; 
    begin 
    with ADOQuery1 do 
    begin 
    sql.Clear; 
    sql.Add('select count(*) as count from info where no=''1'''); 
    open; 
    count:=fieldbyname('count').AsInteger; sql.Clear; 
    sql.Add('select no2 from info where no=''1'''); 
    open; 
    for i:=1 to count do 
    begin 
      arr[i]:=fieldbyname('no2').AsInteger ;//这里在运行时会出错 
    end; 
    不好意思,写错了
      

  4.   

    1楼和2楼说的都没错,改正过来: 
    var 
      i,j,count:integer; 
      arr:array of integer; 
    begin 
    with ADOQuery1 do 
    begin 
    sql.Clear; 
    sql.Add('select count(*) as count from info where no=''1'''); 
    open; 
    count:=fieldbyname('count').AsInteger; setlength(arr, count);
    sql.Clear; 
    sql.Add('select no2 from info where no=''1'''); 
    open; 
    for i:=0 to count-1 do 
    begin 
      arr[i]:=fieldbyname('no2').AsInteger ;//这里在运行时会出错 
    end; 
      

  5.   

    把原来的数组定义改为:arr:array[1..4] of integer;
    刚才写错了arr[i]:=fieldbyname('no2').AsInteger ;//问题出在这里,编译不会报错,但运行程序会出错,它说是无效的整型值,这条语句应该怎么写才是正确的?