Page 1 of 1

MySQL+Zeos. How control errors?

Posted: 03.07.2007, 06:49
by voltron
Hello!
I'm using Lazarus 0.9.20, MySQL 5.0.31 and Zeos 6.6.1 beta for writing database client. How I can show for user errors, which occurs on MySQL server when executing query or stored procedure? I'm trying to using construction:

Code: Select all

try
 ZQuery.ExecSQL;
except
 on E:EZSQLThrowable do
  MsgBox(E.Message,'Error');
  // or MsgBox(E.ErrorCode,'Error');
 end;
It does't work - application don't show message, but on server error occurs - i'm specially use wrong query.
What's wrong?

Sorry for my poor English.

Posted: 03.07.2007, 14:05
by gto
Exaclty, what error ?

Can you post some database info (DDL) and sample SQL that would pop the errors ?

thanks

Posted: 04.07.2007, 12:29
by designshouse
uses ZDbcIntfs;

try
with ZQUERY1 do
begin
SQL.Clear;
SQL.Add('DELETE FROM books WHERE id_book=:Par ');
ParamByName('Par').AsInteger := DBGrid1.DataSource.DataSet.FieldByName('id_book').AsInteger;
ExecSQL;
Close;
end;
except
on E: EZSQLException do
if E.ErrorCode = 1451 then
begin
Application.MessageBox(PChar('Can not delete!'), 'Warning',
mb_Ok or MB_ICONWARNING);
end
else
begin
Application.MessageBox(PChar(E.Message), 'Database Error',
mb_Ok or MB_ICONError);
end;
end;

Posted: 06.07.2007, 12:22
by voltron
2 gto
Some DDL code:

Code: Select all

CREATE TABLE IF NOT EXISTS plots
 (
  CadNom CHAR(19) NOT NULL UNIQUE,
  Area DECIMAL(10,4) NOT NULL,
  Perimeter DECIMAL(10,4) NOT NULL,
  LandCategory TINYINT UNSIGNED NOT NULL,
  TargetUsing VARCHAR(8) NOT NULL,
  Status TINYINT UNSIGNED,
  PRIMARY KEY(CadNom),
  FOREIGN KEY(LandCategory) REFERENCES kz_codes(Id),
  FOREIGN KEY(TargetUsing) REFERENCES cv_codes(Id),
  FOREIGN KEY(Status) REFERENCES statuses(Id)
 ) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS lands
 (
  Id BIGINT UNSIGNED AUTO_INCREMENT,
  CadNom CHAR(19) NOT NULL,
  Area DECIMAL(10,4) NOT NULL,
  Perimeter DECIMAL(10,4) NOT NULL,
  LandType CHAR(4) NOT NULL,
  Status TINYINT UNSIGNED,
  PRIMARY KEY(Id),
  FOREIGN KEY(CadNom) REFERENCES plots(CadNom),
  FOREIGN KEY(LandType) REFERENCES cn_codes(Id),
  FOREIGN KEY(Status) REFERENCES statuses(Id)
 ) ENGINE=InnoDB;
Error occurs, for example, when inserting record to lands table with LandType value than don't satisfy constraint check. Also for error generation i'm using simple UDF that raises error (see here).

2 designshouse
Thanks, i'm try your code today

Posted: 07.07.2007, 16:33
by dhongu
I have some problem with UDF for raise error.

In unit ZDbcMySqlUtils in procedure CheckMySQLError replace And with OR

[syntax="Delphi"]
procedure CheckMySQLError(PlainDriver: IZMySQLPlainDriver;
Handle: PZMySQLConnect; LogCategory: TZLoggingCategory; const LogMessage: string);
var
ErrorMessage: string;
ErrorCode: Integer;
begin
ErrorMessage := Trim(StrPas(PlainDriver.GetLastError(Handle)));
ErrorCode := PlainDriver.GetLastErrorCode(Handle);
if (ErrorCode <> 0) OR (ErrorMessage <> '') then // <-------------
begin
if SilentMySQLError > 0 then
raise EZMySQLSilentException.CreateFmt(SSQLError1, [ErrorMessage]);

DriverManager.LogError(LogCategory, PlainDriver.GetProtocol, LogMessage,
ErrorCode, ErrorMessage);

raise EZSQLException.CreateWithCode(ErrorCode, ErrorMessage);
// Format(SSQLError1, [ErrorMessage]));
end;
end;



[/syntax]

Posted: 10.07.2007, 07:17
by voltron
Thanks!! Now it works!
Only one problem (or feature)... This is not on principle... After replace AND with OR errors intercepts succesfully, but it handles twice...

Posted: 10.07.2007, 12:24
by gto
I think this will do the job:

Code: Select all

procedure CheckMySQLError(PlainDriver: IZMySQLPlainDriver;
  Handle: PZMySQLConnect; LogCategory: TZLoggingCategory; const LogMessage: string);
var
  ErrorMessage: string;
  ErrorCode: Integer;
begin
  ErrorMessage := Trim(StrPas(PlainDriver.GetLastError(Handle)));
  ErrorCode := PlainDriver.GetLastErrorCode(Handle);
  if (ErrorCode <> 0) OR (ErrorMessage <> '') then
  begin
    DriverManager.LogError(LogCategory, PlainDriver.GetProtocol, LogMessage,
      ErrorCode, ErrorMessage);
    if SilentMySQLError > 0 then
      raise EZMySQLSilentException.CreateFmt(SSQLError1, [ErrorMessage])
    else
      raise EZSQLException.CreateWithCode(ErrorCode, ErrorMessage);
  end;
end;