Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread How to iterate through field table definitions ?
Wed, Aug 7 2013 5:03 PMPermanent Link

kamran

HI

Can I  update a dbisam table using fieldefs and if so how ?
eg an example ?

I think the idea would be to have a for loop to make the code tidy instead of one line for each
fieldname that needs to be updated.

Something like :

for i:= 1 to for i := 0 to FieldDefs.Count - 1 do
begin
 iterate through all field names and update with a value
end;

Kind  Regards


Kamran
Wed, Aug 7 2013 9:30 PMPermanent Link

Raul

Team Elevate Team Elevate

Are you talking about updating the table definition or updating the
actual table data.

Fielddefs are mainly used for table changes  (like adding/removing or
changing columns :
http://www.elevatesoft.com/manual?action=viewtopic&id=dbisam4&product=rsdelphiwin32&version=XE4&topic=Creating_Altering_Tables)

I highly doubt you can make the code "tidy" using this - you would still
need to know what fields mean and what type they are.
Exception is cases where you need to mass copy/update or dump table
contents to some other format.

Just a made up example would be something like this (i just typed it so
might need cleanup to compile):

//assume you have a open TDataSet called myDS

while not myDS.EOF do
begin
   myDS.Edit;
   for I:=0 to myDS.FieldCount-1 do
        begin
          //myDS.Fields[I].DisplayName); //fieldname

          if myDs.Fields[I].DataType = ftString then
            myDS.Fields[I].AsString := ''      
          else if myDs.Fields[I].DataType = ftInteger then
             myDS.Fields[I].AsInteger := 0
        end;
        myDS.Post;
        myDS.Next;
end;


this loops thru the table records and then for each it loops thru all
fields and sets strings to '' and integer fields to 0.

Raul

On 8/7/2013 5:03 PM, kamran wrote:
> Can I  update a dbisam table using fieldefs and if so how ?
>
> I think the idea would be to have a for loop to make the code tidy instead of one line for each
> fieldname that needs to be updated.
>
Thu, Aug 8 2013 9:16 AMPermanent Link

kamran

Thanks Raul - that's great - yes it was for a mass update so i did not want to keep typing every single field name (in this case 63 fields) so i guess i was looking for that 'elusive'  easier method.

Kamran


Raul wrote:

Are you talking about updating the table definition or updating the
actual table data.

Fielddefs are mainly used for table changes  (like adding/removing or
changing columns :
http://www.elevatesoft.com/manual?action=viewtopic&id=dbisam4&product=rsdelphiwin32&version=XE4&topic=Creating_Altering_Tables)

I highly doubt you can make the code "tidy" using this - you would still
need to know what fields mean and what type they are.
Exception is cases where you need to mass copy/update or dump table
contents to some other format.

Just a made up example would be something like this (i just typed it so
might need cleanup to compile):

//assume you have a open TDataSet called myDS

while not myDS.EOF do
begin
   myDS.Edit;
   for I:=0 to myDS.FieldCount-1 do
        begin
          //myDS.Fields[I].DisplayName); //fieldname

          if myDs.Fields[I].DataType = ftString then
            myDS.Fields[I].AsString := ''      
          else if myDs.Fields[I].DataType = ftInteger then
             myDS.Fields[I].AsInteger := 0
        end;
        myDS.Post;
        myDS.Next;
end;


this loops thru the table records and then for each it loops thru all
fields and sets strings to '' and integer fields to 0.

Raul

On 8/7/2013 5:03 PM, kamran wrote:
> Can I  update a dbisam table using fieldefs and if so how ?
>
> I think the idea would be to have a for loop to make the code tidy instead of one line for each
> fieldname that needs to be updated.
>
Thu, Aug 8 2013 9:43 AMPermanent Link

Raul

Team Elevate Team Elevate


Doing this thru a sql statement would likely be faster.

Of course you'd need to include all fields - if you do this a lot then
using the fielddefs you can build the sql update statement runtime in
code then execute it as sql

Raul

On 8/8/2013 9:16 AM, kamran wrote:
> Thanks Raul - that's great - yes it was for a mass update so i did not want to keep typing every single field name (in this case 63 fields) so i guess i was looking for that 'elusive'  easier method.
Image