I am having a Database connected application.
Connected using BDE components and database is MySQL.
Now I am doing some validations for checking if duplicate entry while insert statement is executed.
The following is the code to check if duplicate entry for primary key attribute .
var
error :string;
cmpres:integer;
begin
...
/* all my data queries */
try
Query1.ExecSQL;
Except
on E: Exception do
/*check if its a Duplicate entry or other exception*/
begin
error := E.Message;
error := copy(error,length(error)-16,length(error)-2);
cmpres :=CompareStr(LowerCase(error),'for key '+#39+'primary'+#39) ;
if cmpres = 0 then
MessageDlg('Entry already exist',mtError,[mbok],0)
else
MessageDlg('Invalid Data Entries',mtError,[mbok],0);
exit;
end;
end;
end;
The database exceptions are broadly put inside EDatabaseError
Can anyone tell me is there any way I can differentiate between these errors without doing the Message comparisons.
Thanks in advance
If you’re using BDE, the exceptions should be EDBEngineError, not EDatabaseError. You can do this: