Page 1 of 1

Best way of preventing onposterror...onediterror

Posted: Sat 07 Oct 2006 08:29
by mierlp
hi,

I'm new to MySql and MyDAC and used DBISAM before. Therefore i have some questions because i think i'm doing something wrong.

Within DBISAM i defined in a datamodule some constants, like

const
{Declare constants we're interested in}
DBISAM_RECLOCKFAILED =10258;
DBISAM_KEYORRECDELETED =8708;
DBISAM_INDEXCORRUPT =8965;
DBISAM_KEYVIOL =9729;


On the query.onposterror event i used this piece of code for :

procedure TdmTables.tbl_nawPostError(DataSet: TDataSet; E: EDatabaseError;
var Action: TDataAction);
begin
if (E is EDBISAMEngineError) then begin
if (EDBISAMEngineError(E).ErrorCode=DBISAM_KEYORRECDELETED) then begin
Application.MessageBox('You can't delete this record because it's in use on a other computer !'''+#10+'Record in use.', 'Warning', MB_OK+MB_ICONEXCLAMATION+MB_DEFBUTTON1+MB_APPLMODAL);
Action:=daAbort;
end;
end;
end;


Using this kind of solutions i could prevent deleting records which are
in use.

Questions :
1. is there somewhere an example of doing this in MySQL/MyDac
2. are there some errorcodes for mysql...looked...but couldn't find them
om mysql site
3. what's the best way of building a application using mysql/mydac, do you need to use transaction, commit, rollback

Greetz...and thankx
Peter

Posted: Mon 09 Oct 2006 10:27
by Antaeus
1) You can work in the similar way with MyDAC and MySQL. Just add MyClasses unit to the uses clause and replace EDBISAMEngineError with EMyError.
2) Please refer to this topic of MySQL Reference Manual.
3) You are not required to use transactions with MyDAC. You should decide yourself if it is necessary for you depending on your task.

Posted: Tue 10 Oct 2006 14:35
by mierlp
Hi,

Thanks...i.I just tested the solution, but it didn't work. What did i do ?
- create table called ''Priority''
- tables has 2 fields called pri_id (int) and pri_name (var)
- create index called idxPriName, indexfield = pri_name and type = unique
- define const in the datamodule like :

const
ER_DUP_KEY = 1061;

Within the datamodule on Query_Priority.OnPostError just this code :

procedure TdmTables.qry_catPostError(DataSet: TDataSet; E: EDatabaseError;
var Action: TDataAction);
begin
if (E is EMyError) then begin
if (EMyError(E).ErrorCode=ER_DUP_KEY) then begin
Application.MessageBox('De door u ingevoerde waarde bestaat reeds'+#10+'en mag niet dubbel worden geregistreerd!.', 'Mededeling', MB_OK+MB_ICONEXCLAMATION+MB_DEFBUTTON1+MB_APPLMODAL);
dmTables.qry_cat.cancel;
Abort;
end;
end;
end;


Now run the apps and enter the values 'Low' as a newe value
Appending new record and enter again the value 'Low';

Now i get the message : #23000Duplicate key entry 'Low' for key 2
This is correct, because the value 'Low' may only exists 1 time.
BUT...why doesn't the program not check for this error in the
procedure i wrote above ?

Has someone a working example or can explain me what i'm
doing wrong.

Thanks

Posted: Tue 10 Oct 2006 15:38
by Antaeus
This looks like you use wrong error code. Try to change 1061 value with 1062 one.