I'm trying to find out if a record exists in database, I run a query select
and I want to get the result, I still can't get it...
Database
By executing the following query directly on the database:
SELECT T.ID
FROM TABLA T
WHERE ID=3
Is obtained:
No rows returned
Now I try to display a message in Delphi saying that the record does not exist.
In the form I have a type component TQuery
called qValidacion
successfully connected to Oracle 11g database.
attempt 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 qValidacion.IsEmpty then //No se como validar si el Select regresa algo, IsEmpty me muestra los registros existentes
begin
ShowMessage('El registro No Existe');
end;
qValidacion.SQL.Clear;
end;
attempt 2
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('El registro No Existe'); //deberia mostrar el mensaje, pero no lo muestra
end;
qValidacion.SQL.Clear;
end;
The
IsEmpty
TDataSet property returns true if the dataset is empty.In general, with any TDataSet descendant component this should work:
Now,
TQuery
it's a component that's part of the first standard socket layer that came with Delphi, called the Borland Database Engine (BDE), and it's been marked deprecated many years ago. It's likely that the BDE oracle driver is having issues (or always has, I can't remember), and is fetching a row with all the fields innull
it (I remember seeing this behavior with Firebird at some point.You can do two things:
With BDE and a driver that returns a row with null values
If this is the case, then check that the retrieved value is not null, for example this would work in the particular case you raise:
Note that this solution cannot be generalized, since there may be a case where you retrieve a value
null
from a row that does exist in the database, so another option would be to count the records, for example:Move to a modern database connection layer
This is the real answer. Even if you're using old versions of Delphi, like Delphi 7, you can still use DBExpress , or ideally, if you have a modern version of Delphi, the current recommendation is to use FireDac
Another thing that should work is something like this:
But the IsEmpty you mention should also work. Try it to see if you have more luck with this one.
All the best.
I'm having a similar problem... here's my code:
But this does not work, that is, when the data is there it tells me that it is not there and vice versa.....
How can I solve that???