1
votes

DataBase

When I run the following query in a database:

SELECT T.ID
FROM TABLA T
WHERE ID=3

Result:

No rows returned

Now I try show message in Delphi say "The record not exist".

In the form I have a component TQuery call qValidacion successfully connected with database Oracle 11g.

Try 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
I don't follow. You have a query that returns no rows. What exactly are you asking?David Heffernan
Have you checked to be sure that there is at least one row in TABLA which has ID = 3?Bob Jarvis - Reinstate Monica
You've written if (not qValidacion.Eof)... - you want, however to check if you are at Eof, not if you are not at Eof. The idiomatic way to check this, however, is to examine if qValidacion.RecordCount = 0.J...
qValidacion.SQL.Add('SELECT T.ID'); - BAD idea - there may already be something! Either call SQL.Clear before the first line, or just call one-liner SQL.Text := ' SELECT ... WHERE T.ID=3';Arioch 'The
@J... - and download all the data to the client, maybe all the table, and overflow the memory ? the idiomatic way is to call if qValidation.IsEmpty then ...Arioch 'The

1 Answers

1
votes

If you want to check if they are any record in your Query don't use the qValidacion.EOF but qValidacion.IsEmpty

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

The EOF Function is here for returning true when you reach the end of a DataSet. Example:

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

Edit1: Using IsEmpty is indeed more clean. Thanks to Arioch 'The