MyDAC & C++Builder

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
saidus

MyDAC & C++Builder

Post by saidus » Tue 25 Oct 2005 14:44

Hi !!
I have 2 Tables : SALE & DSALE Master / Detail in myisam format
the probleme is that i can't delete the detail rows when i delete the master row even when i use the onbeforepost event for the master Query
(I used 2 TMyQuery components)
so could you help me resolve this ???
thanks for all

alec
Posts: 20
Joined: Wed 24 Aug 2005 09:28
Location: Africa

Re: MyDAC & C++Builder

Post by alec » Wed 26 Oct 2005 04:00

saidus wrote:onbeforepost event
Maybe OnBeforeDelete event

saidus

Post by saidus » Wed 26 Oct 2005 08:37

yes yes yes
i used & use on before delete event!!but don't work

GEswin
Posts: 186
Joined: Wed 03 Nov 2004 16:57
Location: Spain
Contact:

Post by GEswin » Wed 26 Oct 2005 09:51

Perhaps post what you do exactly onBeforeDelete event

saidus
Posts: 78
Joined: Wed 26 Oct 2005 09:54
Location: Algeria

Post by saidus » Wed 26 Oct 2005 10:00

On the OnBeforePost and OnBeforeDelete event Detail table i poste some information on Master table

GEswin
Posts: 186
Joined: Wed 03 Nov 2004 16:57
Location: Spain
Contact:

Post by GEswin » Wed 26 Oct 2005 12:49

on master query you should do something similar to this,

Code: Select all

procedure TFrmPacients.DBQpacfitBeforeDelete(DataSet: TDataSet);
begin

 Connection.ExecSQL('delete from detailtable where field = '+BQpacfitcodigo_paciente.AsString,[]);

end;
When you try to delete master field, it deletes details (Of course you have to set correct query).

alec
Posts: 20
Joined: Wed 24 Aug 2005 09:28
Location: Africa

Post by alec » Thu 27 Oct 2005 04:53

More easy way for delete details:

Code: Select all

MyQueryMaster->SQLDelete = "DELETE master, detail FROM master, detail
WHERE  master.id = detail.master_id and master.id = :Old_id";
or in designtime
MyQuery editor :: Udate SQLs :: Delete

Code: Select all

DELETE master, detail FROM master, detail
WHERE  master.id = detail.master_id and master.id = :Old_id
Note: In this case MyQueryMaster->Options->StrictUpdate must be false

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Thu 27 Oct 2005 06:23

> i can't delete the detail rows

Please describe it more detailed. Which error message do you get?

Guest

Post by Guest » Thu 27 Oct 2005 12:25

Hello !!!
I have a memory access error msg !!
the real problem is :
when deleting master record i use this code in OnBeforeDelete event

Code: Select all

  TableDetail->First(); 
while(!TableDetail->Eof) 
{ 
    TableDetail->Delete(); 
} 
((( note : MasterTable = TMyQuery , DetailTable = TMyTable )))
but in OnAfterDelete and OnAfterPost detail table i have to calculate some value to store in Master table like this

Code: Select all

  
          Extended tot = 0; 
          if(MasterTable->State!=dsEdit) 
             MasterTable->Edit(); 
             DetailTable->First(); 
          while(!DetailTable->Eof) 
          { 
             tot+=DetailTableQT->Value; 
                  DetailTable->Next(); 
          } 
          MasterTableQT->Value    = tot; 
          MasterTable->Post();
thnks for help

alec
Posts: 20
Joined: Wed 24 Aug 2005 09:28
Location: Africa

Post by alec » Fri 28 Oct 2005 12:17

You can use (for MySQL 4.1+):

Code: Select all

SQL: update master set qt = (select sum(detail.qt) from detail where detail.master_id=master.id ) where id= :MasterId
MasterTable->RefreshRecord();
instead of

Code: Select all

Extended tot = 0; 
          if(MasterTable->State!=dsEdit) 
             MasterTable->Edit(); 
             DetailTable->First(); 
          while(!DetailTable->Eof) 
          { 
             tot+=DetailTableQT->Value; 
                  DetailTable->Next(); 
          } 
          MasterTableQT->Value    = tot; 
          MasterTable->Post();
and http://www.crlab.com/forums/viewtopic.php?t=2139#7116
instead of

Code: Select all

TableDetail->First(); 
while(!TableDetail->Eof) 
{ 
    TableDetail->Delete(); 
} 
It must increase perfomance of your application. Sorry for nag :)

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Mon 31 Oct 2005 15:07

> I have a memory access error msg

You should be very careful on writing event handler that changes data. Do not call Cancel, Post, Next, Delete etc in BeforeX or AfterY event handlers.

If you send us (mydac*crlab*com) a complete small sample we will try to answer more detailed.

Post Reply