我在我的應用程序中使用了2 TDBGrid
控件。如何在2個不同(但相同結構)的數據庫表中編輯2行 - Delphi
第1個DBGrid顯示來自名爲
Orders
的表中的數據。第二個DBGrid顯示來自名爲
Archive
的表中的數據。
兩個表是在同一數據庫中,並且具有相同的結構(相同數量和列的名稱,以及相同的設置)。
下面是兩個表的結構:
我用2個行動在我的應用程序:
的第一個動作是
Add Order
。第二個動作是Edit Order
。
當我使用Add Order
表單時,它成功添加了我在兩個表中的新行中輸入的詳細信息。
所以這個工程。
現在,我想選擇其中一行添加並編輯其信息。我使用Edit Order
這個動作。它顯示了一個與要編輯和保存的信息類似的表單。它應該編輯兩個表中的信息。它的確如此,但ID改變了,我最終在第一個DBGrid上編輯了一些內容,而在第二個DBGrid中,它將信息更改爲其他內容(來自另一個ID)。
這裏是我的Edit
動作代碼:
procedure TForm2.actEditComandaExecute(Sender: TObject);
begin
if (dbmodule.SQLConnection1.Connected) and (dbmodule.comenziDataSet.IsEmpty = false) then
begin
editcustomerform.Edit1.Text := dbmodule.comenziDataSetstare.Value;
editcustomerform.Edit2.Text := dbmodule.comenziDataSetclient.Value;
editcustomerform.Edit3.Text := dbmodule.comenziDataSettelefon.Value;
editcustomerform.Edit4.Text := dbmodule.comenziDataSetemail.Value;
editcustomerform.Edit5.Text := dbmodule.comenziDataSetdetalii.Value;
editcustomerform.Edit6.Text := dbmodule.comenziDataSetpret.Value;
editcustomerform.Edit7.Text := dbmodule.comenziDataSetlivrare.Value;
editcustomerform.Edit8.Text := dbmodule.comenziDataSetuser.Value;
editcustomerform.Edit9.Text := dbmodule.comenziDataSetstatus.Value;
if editcustomerform.ShowModal = mrOk then
begin
dbmodule.SQLQuery1.SQL.Clear;
dbmodule.SQLQuery1.SQL.Add('UPDATE `tipotask`.`comenzi` SET `stare`=''' + editcustomerform.Edit1.Text + ''', `client`=''' + editcustomerform.Edit2.Text + ''', `telefon`=''' + editcustomerform.Edit3.Text + ''', `email`=''' + editcustomerform.Edit4.Text + ''', `detalii`=''' + editcustomerform.Edit5.Text + ''', `pret`=''' + editcustomerform.Edit6.Text + ''', `livrare`=''' + editcustomerform.Edit7.Text + ''', `user`=''' + editcustomerform.Edit8.Text + ''', `status`=''' + editcustomerform.Edit9.Text + ''' WHERE `id`=''' + IntToStr(dbmodule.comenziDataSetid.Value) + ''';');
dbmodule.SQLQuery1.ExecSQL(true);
dbmodule.SQLQuery3.SQL.Clear;
dbmodule.SQLQuery3.SQL.Add('UPDATE `tipotask`.`arhiva` SET `stare`=''' + editcustomerform.Edit1.Text + ''', `client`=''' + editcustomerform.Edit2.Text + ''', `telefon`=''' + editcustomerform.Edit3.Text + ''', `email`=''' + editcustomerform.Edit4.Text + ''', `detalii`=''' + editcustomerform.Edit5.Text + ''', `pret`=''' + editcustomerform.Edit6.Text + ''', `livrare`=''' + editcustomerform.Edit7.Text + ''', `user`=''' + editcustomerform.Edit8.Text + ''', `status`=''' + editcustomerform.Edit9.Text + ''' WHERE `id`=''' + IntToStr(dbmodule.arhivaDataSetid.Value) + ''';');
dbmodule.SQLQuery3.ExecSQL(true);
//we need to refresh the data
actRefreshData.Execute;
end;
end;
這裏是發生了什麼事的視頻。請注意ID列如何改變和弄亂事情。
video removed - no need - solved
的最終目標是增加使用Add Order
形式,效果很好,這增加的順序2表中的一個新的秩序。這很好。
每當我需要訂購Edit
訂單,我只需在Orders
DBGrid中選擇它並對其進行編輯。一旦我這樣做,它也應該更改Archive
表中的相應信息 - 只是我正在編輯的記錄。
我該如何解決這個問題?
我已經在它上了幾個小時了,我無法弄清楚。我不理解ID字段(自動增量,主要,唯一,索引等)。我似乎無法把頭圍住它。
我很新,所以請儘量提供完整的解釋。
您使用'AdoTable'? – Sami
爲什麼在地球上使用數據控件(TDBGrid)來顯示數據,但是沒有數據庫感知的TEDits來編輯它?爲什麼不使用TDBEdits? – MartynA
@Sami,他使用dbExpress(TSQLConnection而不是TADOConnection)。 –