I'm super new at coding and I'm making a simple apps now.
So as you can see in the pic, I have multiple data in selected date. If I wanted to update every row with different values and do it with save button, what code should I do?
Does my dbgrid need more setting at its properties?
I'm using Delphi 7 and zeos as db connection as well as oracle as db. Thanks in advance.
Use an Update Query
select "Database"
Update "tablename"
set "colonnename1"="new value, "colonnename1"="new value, ...
where "colonnename"=value
this is the update query and while using delphi you can do this :
begin
uniquery1.clear;
Uniquery1.SQL.Add('update person set name ='''+edit1.Text+''',lastname='''+edit2.Text+''',age='+edit3.Text+',city='''+edit4.Text+''' where date=' + edit5.text);
uniquery1.ExecSQL;
messagedlg('Successfully Modified!',mtinformation,[mbOK],0);
end;
Or you can use the parametrized method which is way better then concatenating the SQL As NIL said :
begin
Uniquery1.sql.clear;
Uniquery1.SQL.Add('UPDATE person SET "1colonnename"= ":parmID", name= :sal WHERE Num='+ inttostr(strtoint(dbgrid1.DataSource.DataSet.FieldValues['num']))); uniquery1.ParamByName('paramID').value:=edit1.text;
uniquery1.ParamByName('sal').Value:=edit2.text;
uniquery1.ExecSQL;
end;
This is just an exemple but this how it works ! Good luck
There are different ways depends of what you want and what you use.
If you wish to edit several rows into DBGrid and save them simultaneously, most dataset components supports a CachedUpdate
mode (look into the Zeos documentation for details). In this mode dataset holds all changes made until you call ApplyUpdates
method to put these changes into DB using one batch.
Using Oracle, you can do it without cached updates, just use 'long transaction'. In this case you should start transaction when form opens, edit and post data at usual way and commit them when Save
pressed. This way have some disadvantages, but I doubt you'll face them.
If you wish to do some sort of mass update when pressing Save
button, you have two ways. First of all, you can set up a query component, set it's SQL property into something like
update My_Table set My_Field_1 = :Value1, My_Field_2 = :Value2
and call it into save action handler:
qUpdate.Params.ParamByName('Value1').AsString := edFirstValue.Text;
qUpdate.Params.ParamByName('Value2').AsString := edSecondValue.Text;
qUpdate.ExecSQL;
qMain.Refresh; { we're in need to see updates values, yeah? }
Otherwise, you can do it from client:
qMain.DisableControls;
try
Bookmark := qMain.Bookmark;
qMain.First;
while not qMain.Eof do
begin
qMain.Edit;
qMain.FieldByName('My_Field_1').AsString := edFirstValue.Text;
qMain.FieldByName('My_Field_2').AsString := edSecondValue.Text;
qMain.Post;
qMain.Next;
end;
finally
qMain.Bookmark := Bookmark;
qMain.EnableControls;
end;
That's, generally, not so good way, so don't addict of that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.