MySQL+Zeos. How control errors?

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
voltron
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 02.07.2007, 07:19
Location: Ukraine

MySQL+Zeos. How control errors?

Post 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.
gto
Zeos Dev Team
Zeos Dev Team
Posts: 278
Joined: 11.11.2005, 18:35
Location: Porto Alegre / Brasil

Post by gto »

Exaclty, what error ?

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

thanks
Use the FU!!!!!IN Google !

gto's Zeos Quick Start Guide

Te Amo Taís!
designshouse
Fresh Boarder
Fresh Boarder
Posts: 20
Joined: 21.11.2005, 10:13
Location: Pieštany
Contact:

Post 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;
voltron
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 02.07.2007, 07:19
Location: Ukraine

Post 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
User avatar
dhongu
Junior Boarder
Junior Boarder
Posts: 37
Joined: 28.09.2005, 08:37
Location: Bucuresti
Contact:

Post 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]
Dorin Hongu
voltron
Fresh Boarder
Fresh Boarder
Posts: 3
Joined: 02.07.2007, 07:19
Location: Ukraine

Post 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...
gto
Zeos Dev Team
Zeos Dev Team
Posts: 278
Joined: 11.11.2005, 18:35
Location: Porto Alegre / Brasil

Post 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;
Use the FU!!!!!IN Google !

gto's Zeos Quick Start Guide

Te Amo Taís!
Post Reply