我无法删除使用Delphi7处于关系中的数据库的2个表中的所有数据

时间:2022-10-09 18:05:40

I am getting the following error :

我收到以下错误:

Project PAT_p.exe raised exception class EOleException with message 'The record cannot be deleted or changed because table 'tblAntwoorde' includes related records'. Process stopped. Use Step or Run to continue.

项目PAT_p.exe引发异常类EOleException并显示消息'由于表'tblAntwoorde'包含相关记录',因此无法删除或更改记录。流程停止了。使用“步骤”或“运行”继续。

This error occurs at the first execute in the following code:

在以下代码中的第一次执行时发生此错误:

procedure TfrmKomp.btnTerug1Click(Sender: TObject);
begin
  frmData.qryVGKompetisieDB.Active := false;
  frmData.qryVGKompetisieDB.SQL.Text := 'Delete * from tblDeelnemers'; // delete from table1
  frmData.qryVGKompetisieDB.ExecSQL;
  frmData.qryVGKompetisieDB.SQL.Text := 'Delete * from tblAntwoorde'; // delete from table2
  frmData.qryVGKompetisieDB.ExecSQL;
  frmData.qryVGKompetisieDB.SQL.Text := 'SELECT * from tblDeelnemers'; // for displaying on dbgrid that records is removed  
  frmData.qryVGKompetisieDB.Active := true;
end;

My tables in my database are linked in a one-to-many relationship where ID is the PK in tblDeelname and Nommer is a autonumber and the PK in tblAntwoorde. Unfortunately this is for a Practical assessment task at school and the relationship have to be there.

我的数据库中的表以一对多关系链接,其中ID是tblDeelname中的PK,Nommer是自动编号,PK是tblAntwoorde。不幸的是,这是在学校进行实际评估任务,并且必须存在关系。

I want to remove all the data in the tables but the tables have to stay there with all of its columns. This do not necessarily have to be with a sql statement, any sort of code that I can use in delphi7 will be fine.

我想删除表中的所有数据,但表必须保留其所有列。这不一定是sql语句,我可以在delphi7中使用的任何类型的代码都可以。

1 个解决方案

#1


0  

After the Cascate delete sugestion I whent to research it and found a easier way here.

在Cascate删除消息之后我会研究它并在这里找到一种更简单的方法。

All that basically have to happen is that the data from the secondary Table have to be deleted before those of the primary Table.

所有基本必须发生的事情是,必须在主表的数据之前删除辅助表中的数据。

procedure TfrmKomp.btnTerug1Click(Sender: TObject);
begin
  frmData.qryVGKompetisieDB.Active := false;
  frmData.qryVGKompetisieDB.SQL.Text := 'Delete * from tblAntwoorde'; // delete from SecondaryTable
  frmData.qryVGKompetisieDB.ExecSQL;
  frmData.qryVGKompetisieDB.SQL.Text := 'Delete * from tblDeelnemers'; // delete from PrimaryTable
  frmData.qryVGKompetisieDB.ExecSQL;
  frmData.qryVGKompetisieDB.SQL.Text := 'SELECT * from tblDeelnemers'; // for displaying on dbgrid that records is removed  
  frmData.qryVGKompetisieDB.Active := true;
end;

Tanks for all the help with your comments guys. I really appreciate it. ☺

坦克为你的评论员提供所有帮助。对此,我真的非常感激。 ☺

#1


0  

After the Cascate delete sugestion I whent to research it and found a easier way here.

在Cascate删除消息之后我会研究它并在这里找到一种更简单的方法。

All that basically have to happen is that the data from the secondary Table have to be deleted before those of the primary Table.

所有基本必须发生的事情是,必须在主表的数据之前删除辅助表中的数据。

procedure TfrmKomp.btnTerug1Click(Sender: TObject);
begin
  frmData.qryVGKompetisieDB.Active := false;
  frmData.qryVGKompetisieDB.SQL.Text := 'Delete * from tblAntwoorde'; // delete from SecondaryTable
  frmData.qryVGKompetisieDB.ExecSQL;
  frmData.qryVGKompetisieDB.SQL.Text := 'Delete * from tblDeelnemers'; // delete from PrimaryTable
  frmData.qryVGKompetisieDB.ExecSQL;
  frmData.qryVGKompetisieDB.SQL.Text := 'SELECT * from tblDeelnemers'; // for displaying on dbgrid that records is removed  
  frmData.qryVGKompetisieDB.Active := true;
end;

Tanks for all the help with your comments guys. I really appreciate it. ☺

坦克为你的评论员提供所有帮助。对此,我真的非常感激。 ☺