query以后如何得到数据库纪录如何写入listview的问题!在线!急(50分)

  • 主题发起人 江南大米
  • 开始时间

江南大米

Unregistered / Unconfirmed
GUEST, unregistred user!
select `a1`,`a2`,`a3` from `A` where `a4`=1 group by `a1`;
接着写到listview中该怎么写啊!

a1 a2 a3
急!!!急!!![?]

 
用循环语句。逐条写入
 
var lt:Tlistitem;
begin
...
while not query.eof do
begin
lt:=listview1.litems.add;
lt.caption:=query.fieldbyname('a1').asstring;
lt.subitems.add(query.fieldbyname('a2').asstring;);
lt.subitems.add(query.fieldbyname('a3').asstring;);
query.next
end;
 
迟来了一步
 
迟了两步
 
不要唱周杰伦的星晴了!
我的SELCET语句是这么加的
VAR str1:string;
begin
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(str1);
lt.Caption:=query1.fieldbyname('a1').asstring;//问题在这里a1在真正的库中是[red]中文名[/red]!
//我用'`字段a1`'出错了!听说有个取别名办法,SQL怎么写!
Query1.Prepare;
Query1.ExecSQL;
end;
 
提示错误是field '`字段a1`'no found
 
select b1 = a1 from A where .....

FieldByName('b1').asstring

try it
 
Select `字段1`,`字段2`,`字段3` from `表1` where `字段4`=1 group by `字段1`
这一句保证没有问题!关键是添加到listview里出问题了!代码如上!
lt.Caption:=query1.[red]fieldbyname('字段1')[/red].asstring;
红色部分DELPHI提示里没有,我自己手工写的!???不知道为什么!??
 
将It.Caption:=query1.[red]fieldbyname('字段1')[/red].asstring;句中
的It.Caption改为edit1.text试试!如果不行就是数据库对中文支持不好!
那就用字典算了!
 
select 表里的字段名 as A1 from 你的表

query.fieldbyname('a1').asstring;
就可以了。
 
还是老问题啊!哎以为一个很简单的问题竟然没有解决!
 
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(str1);
lt.Caption:=query1.fieldbyname('a1').asstring;//问题在这里a1在真正的库中是[red]中文名[/red]!
//我用'`字段a1`'出错了!听说有个取别名办法,SQL怎么写!
Query1.Prepare;
Query1.ExecSQL;
看看这样行不行。。。。
Query.Fields.Fields[0].AsString
还有。你还没打开Query就读好象不太好吧。
应该先。。。。
Query.Sql.text := '..........';
Query.Open;
Label1.caption := Query.Fields.Fields[0].AsString;
Query.close;

 
还有一个办法
Select a1=字段一,a2=字段二, a3 as 字段三 from AA Where.....

给字段取个别名
可以用 = 或 As
 
你的语句中a1加上了引号,不要加引号,字段名是不要加引号的
 
如果是在sql server中 列名可以这样传递 [a1],[a2]....
 
mysql!头痛了两天了!快炸了!兄弟十万火急啊![:(]
 
select [red]中文名[/red] A1 from 表名;
select [red]中文名[/red] as A1 from 表名;//加as对有的数据库系统会出错


 
function ShowListView(ClientDataSet : TClientDataSet; ListView : TListView; ImageIndex : Integer) : Integer;
var
i, j : Integer;
ListItem : TListItem;
begin
ListView.Items.Clear;
with ClientDataSet do
begin
if Active = True
then
First
else
Open;
for i := 0 to RecordCount - 1 do
begin
with ListView.Items do
begin
ListItem := Add ;
for j := 0 to Fields.Count - 1 do
With Item do
begin
if j = 0 then
ListItem.Caption := Fields[j].AsString
else
begin
if Fields[j].DataType = ftDateTime then
begin
if TimeToStr(Fields[j].AsDateTime) = '0:00:00' then
ListItem.SubItems.Add(Fields[j].AsString + ' 0:00:00')
else
ListItem.SubItems.Add(Fields[j].AsString);
end
else
ListItem.SubItems.Add(Fields[j].AsString);
end;
end;
ListItem.ImageIndex := ImageIndex;
Next;
end;
end;
end;
Result := 0;
end;
 
顶部