“字段“COUNT(id)”没有数据集” Delphi 中的 TSQLQuery

发布于 2024-08-10 09:59:36 字数 822 浏览 9 评论 0原文

抱歉我的英语不好,但我希望你能理解我:P

我正在尝试在代码中创建新的 TSQLQuery 组件,而不是将其放在表单上。我写了该代码:

var
sql:tsqlquery;
pole:TFMTBCDField;

....
   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   pole:=TFMTBCDField.Create(self);
   pole.Name:='sqlilerap';
   pole.FieldName:='COUNT(idrap)';
   pole.FieldKind:=fkData;
   pole.DisplayLabel:='COUNT(idrap)';

   sql.Fields.Add(pole);
   sql.Open;
   showmessage(sql.FieldByName('COUNT(idrap)').AsString);
   sql.Free;
   pole.Free;

但是当我尝试访问数据时出现异常:

First chance exception at $75999617. Exception class EDatabaseError with message 'Field 'COUNT(idrap)' has no dataset'. Process htstrm2.exe (2308)

我应该做什么?

Sorry for my english, but i hope you'll understand me :P

I'm trying to create new TSQLQuery component in code, without placing it on form. I wrote that code:

var
sql:tsqlquery;
pole:TFMTBCDField;

....
   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   pole:=TFMTBCDField.Create(self);
   pole.Name:='sqlilerap';
   pole.FieldName:='COUNT(idrap)';
   pole.FieldKind:=fkData;
   pole.DisplayLabel:='COUNT(idrap)';

   sql.Fields.Add(pole);
   sql.Open;
   showmessage(sql.FieldByName('COUNT(idrap)').AsString);
   sql.Free;
   pole.Free;

but i'm getting exception when i try to access data:

First chance exception at $75999617. Exception class EDatabaseError with message 'Field 'COUNT(idrap)' has no dataset'. Process htstrm2.exe (2308)

What should I do ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧话新听 2024-08-17 09:59:36

甚至不做一个领域。诸如此类的查询仅返回一个字段。所以只需从 fields 数组中引用:

var
sql:tsqlquery;

....
   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   sql.Open;
   showmessage(sql.fields[0].AsString);
   sql.Free;

Don't even make a field. Queries such as this return one and only one field. So just reference from the fields array:

var
sql:tsqlquery;

....
   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   sql.Open;
   showmessage(sql.fields[0].AsString);
   sql.Free;
一腔孤↑勇 2024-08-17 09:59:36

您的数据库驱动程序报告聚合表达式的空字段名称。

您的字段的别名:

   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) AS cnt FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   pole:=TFMTBCDField.Create(self);
   pole.Name:='sqlilerap';
   pole.FieldName:='cnt';
   pole.FieldKind:=fkData;
   pole.DisplayLabel:='cnt';

   sql.Fields.Add(pole);
   sql.Open;
   showmessage(sql.FieldByName('cnt').AsString);
   sql.Free;
   pole.Free;

Your database driver reports the empty field name for the aggregate expression.

Alias your field:

   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) AS cnt FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   pole:=TFMTBCDField.Create(self);
   pole.Name:='sqlilerap';
   pole.FieldName:='cnt';
   pole.FieldKind:=fkData;
   pole.DisplayLabel:='cnt';

   sql.Fields.Add(pole);
   sql.Open;
   showmessage(sql.FieldByName('cnt').AsString);
   sql.Free;
   pole.Free;
§普罗旺斯的薰衣草 2024-08-17 09:59:36

您必须将数据集显式分配给该字段,请尝试添加此行

pole.DataSet:=sql;

再见。

You must explicitly assign the dataset to the field, try adding this line

pole.DataSet:=sql;

Bye.

清欢 2024-08-17 09:59:36

为返回的列指定别名。然后您可以通过该别名访问它:

sql.SQL.Text:='SELECT COUNT(idrap) AS iDrapCount FROM raporty WHERE dkier 
="'+lvkierowcy.Selected.Caption+'";';
....
pole.FieldName := 'iDrapCount';

Alias the column being returned. You can then access it by that aliased name:

sql.SQL.Text:='SELECT COUNT(idrap) AS iDrapCount FROM raporty WHERE dkier 
="'+lvkierowcy.Selected.Caption+'";';
....
pole.FieldName := 'iDrapCount';
-黛色若梦 2024-08-17 09:59:36

尝试将此行添加到您的查询中:

AND idrap <> nil

Try adding this line to your query:

AND idrap <> nil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文