Recent

Author Topic: (SOLVED) DBF add fields into existing table  (Read 902 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1886
    • Lazarus and Free Pascal italian community
(SOLVED) DBF add fields into existing table
« on: February 13, 2019, 11:37:18 am »
Hi I want to add a column to my existing table in a dbf file. What am I doing wrong?
Code: Pascal  [Select]
  1. var
  2.    MyDBF: TDBF;
  3.    dbDefs: TDbfFieldDefs;
  4. begin
  5.  
  6.   MyDBF:=TDBF.Create(nil);
  7.   MyDBF.FilePath:=Self.PAthDBF;
  8.   MyDBF.TableName:=Self.TableNameDBF;
  9.  
  10.   if MyDBF.FindField('ELAB') = nil then
  11.   begin
  12.        // create new field list
  13.        MyDBF.Exclusive:= True;
  14.        dbDefs := TDbfFieldDefs.Create(nil);
  15.        try
  16.          dbDefs.Assign(MyDBF.DbfFieldDefs);
  17.          dbDefs.Add('ELAB',ftString,1);
  18.          MyDBF.RestructureTable(dbDefs, true);
  19.        finally
  20.          dbDefs.Free;
  21.        end;
  22.   end;
  23.   MyDBF.Open;
  24.   MyDBF.Close;  
  25.  
« Last Edit: February 14, 2019, 08:40:41 am by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Handoko

  • Hero Member
  • *****
  • Posts: 3085
  • My goal: build my own game engine using Lazarus
Re: DBF add fields into existing table
« Reply #1 on: February 13, 2019, 12:22:49 pm »
I have code to add dbf column. I copy/paste and modify a bit to suit your case:

Code: Pascal  [Select]
  1. function FieldIndex(aDBF: TDBF; const strField: string): Integer;
  2. var
  3.   i: Integer;
  4. begin
  5.   Result := -1;
  6.   for i := 0 to (aDBF.DbfFieldDefs.Count-1) do
  7.     if (aDBF.DbfFieldDefs.Items[i].FieldName = strField) then
  8.     begin
  9.       Result := i;
  10.       Exit;
  11.     end;
  12. end;
  13.  
  14. //...
  15.  
  16. var
  17.   MyDBF  : TDBF;
  18.   dbDefs : TDbfFieldDefs;
  19.   dbDef  : TDbfFieldDef;
  20.   isNew  : Boolean;
  21.  
  22. begin
  23.  
  24.   MyDBF           := TDBF.Create(nil);
  25.   MyDBF.FilePath  := 'the_path_to_the_location';
  26.   MyDBF.TableName := 'the_file_name';
  27.   MyDBF.Open;
  28.  
  29.   dbDefs := TDbfFieldDefs.Create(nil);
  30.   dbDefs.Assign(MyDBF.DbfFieldDefs);
  31.  
  32.   isNew := False;
  33.   if FieldIndex('ELAB') < 0 then
  34.   begin
  35.     isNew           := True;
  36.     dbDef           := dbDefs.AddFieldDef;
  37.     dbDef.FieldName := 'ELAB';
  38.     dbDef.Type      := ftString;
  39.     dbDef.Size      := 1;
  40.     dbDef.Required  := False;
  41.   end;
  42.  
  43.   MyDBF.Close;
  44.   if isNew then MyDBF.RestructureTable(dbDefs, True);
  45.   dbDefs.Free;
  46.   MyDBF.Free
  47.  
  48. end;
« Last Edit: February 13, 2019, 12:36:02 pm by Handoko »

xinyiman

  • Hero Member
  • *****
  • Posts: 1886
    • Lazarus and Free Pascal italian community
Re: DBF add fields into existing table
« Reply #2 on: February 14, 2019, 08:40:29 am »
I replace

dbDef.Type      := ftString;

with

dbDef.FieldType := ftString;

But run correctly. Thank you very much.  :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1