一个取数据问题

sunny906 2008-05-30 03:59:26
我的一张数据表是这样的:
表名: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;
...全文
61 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzmhero 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sunny906 的回复:]
把原来的数组定义改为:arr:array[1..4] of integer;
刚才写错了

arr[i]:=fieldbyname('no2').AsInteger ;//问题出在这里,编译不会报错,但运行程序会出错,它说是无效的整型值,这条语句应该怎么写才是正确的?
[/Quote]

数据库中有错误的数据, 字段no2里,有不能转换的字符。
sunny906 2008-05-30
  • 打赏
  • 举报
回复
把原来的数组定义改为:arr:array[1..4] of integer;
刚才写错了

arr[i]:=fieldbyname('no2').AsInteger ;//问题出在这里,编译不会报错,但运行程序会出错,它说是无效的整型值,这条语句应该怎么写才是正确的?
louevy 2008-05-30
  • 打赏
  • 举报
回复
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;
sunny906 2008-05-30
  • 打赏
  • 举报
回复
的一张数据表是这样的:
表名: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;
不好意思,写错了
阿三 2008-05-30
  • 打赏
  • 举报
回复
这个得用二维数组吧,一维的好像满足不了你的需求
gzmhero 2008-05-30
  • 打赏
  • 举报
回复
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;
hongqi162 2008-05-30
  • 打赏
  • 举报
回复
for i:=0 to count-1 do
begin
arr[i]:=fieldbyname('no2').AsInteger ;//下标是0,所以出错了
end;

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧