代码之家  ›  专栏  ›  技术社区  ›  Goerman

如何在Delphi中获取TQuery的结果?

  •  1
  • Goerman  · 技术社区  · 10 年前

    数据库

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

    SELECT T.ID
    FROM TABLA T
    WHERE ID=3
    

    结果:

    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 回复  |  直到 6 年前
        1
  •  1
  •   Sam z    10 年前

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

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

    EOF函数用于在到达数据集末尾时返回true。例子:

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

    编辑1:使用IsEmpty确实更干净。感谢Arioch‘The