如何获得TQuery en Delphi的结果?

时间:2016-01-15 20:51:00

标签: sql oracle delphi

数据库

当我在数据库中运行以下查询时:

SELECT T.ID
FROM TABLA T
WHERE ID=3

Resutl:

No rows returned

现在我在Delphi中尝试显示消息说“记录不存在”。

在表单中我有一个组件TQuery调用qValidacion成功连接数据库Oracle 11g。

尝试1

procedure TfPrueba.ButtonAceptarClick(Sender: TObject);
begin
    qValidacion.Close;
    qValidacion.SQL.Add('SELECT T.ID');
    qValidacion.SQL.Add('FROM TABLA T');
    qValidacion.SQL.Add('WHERE ID=3');
    qValidacion.Open;
    qValidacion.First;
    if (not qValidacion.Eof) then 
        begin
             ShowMessage('The record not exist'); //It Should display the message, but does not show
        end;
    qValidacion.SQL.Clear;
end;

1 个答案:

答案 0 :(得分:1)

如果您想检查他们是否是您的查询中的任何记录,请不要使用qValidacion.EOF但qValidacion.IsEmpty

if (qValidacion.IsEmpty) then 
begin
  ShowMessage('The record not exist'); 
end;

当您到达DataSet的末尾时,EOF函数用于返回true。例如:

qValidacion.First;
while not qValidacion.eof do
begin
 // do Something with the current record.
 qValidacion.next
end;

Edit1:使用IsEmpty确实更干净。感谢Arioch' The