creating TDBF tables lazarus / 16-Databases ---------------------------------------------------------------------- ilniko · 2006-03-01 19:22 Hi all, sorry my ignorance, but how can I create a dbf file, for using with the TDBF component? I've not BDE, so maybe I can create it using OpenOffice Calc, saving into dbf format? In this case, how can I set fields datatypes? Nik ---------------------------------------------------------------------- barko · 2006-03-01 19:50 I use this in one of my unit: Code: [Select] // do modification for yourself... unit mydbs; interface uses sysutils,db,dbf; var       BDETableData:array[1..255] of string;       BDETableSize:byte; procedure CreateMyTable(Table:TDbf;FileName:string); implementation // helping functions Function WordCnt(Str:string):integer; var  W,I: integer;  SpaceBefore: boolean; begin    If Str = '' then    begin        WordCnt := 0;        exit;    end;    SpaceBefore := true;    W := 0;    For  I :=  1 to length(Str) do    begin        If SpaceBefore and (Str[I] <> ' ') then        begin            W := succ(W);            SpaceBefore := false;        end        else            If (SpaceBefore = false) and (Str[I] = ' ') then                SpaceBefore := true;    end;    WordCnt := W; end; Function LocWord(StartAT,Wordno:integer;Str:string):integer; var  W,L: integer;  Spacebefore: boolean; begin    If (Str = '') or (wordno < 1) or (StartAT > length(Str)) then    begin        LocWord := 0;        exit;    end;    SpaceBefore := true;    W := 0;    L := length(Str);    StartAT := pred(StartAT);    While (W < Wordno) and (StartAT <= length(Str)) do    begin        StartAT := succ(StartAT);        If SpaceBefore and (Str[StartAT] <> ' ') then        begin            W := succ(W);            SpaceBefore := false;        end        else            If (SpaceBefore = false) and (Str[StartAT] = ' ') then                SpaceBefore := true;    end;    If W = Wordno then       LocWord := StartAT    else       LocWord := 0; end; Function ExtractWords(StartWord,NoWords:integer;Str:string):string; var Start, finish : integer; begin    If Str = '' then    begin        ExtractWords := '';        exit;    end;    Start := LocWord(1,StartWord,Str);    If Start <> 0 then       finish := LocWord(Start,succ(NoWords),Str)    else    begin        ExtractWords := '';        exit;    end;    If finish = 0 then       finish := succ(length(Str));    Repeat        finish := pred(finish);    Until Str[finish] <> ' ';    ExtractWords := copy(Str,Start,succ(finish-Start)); end; procedure CreateMyTable(Table:TDbf;FileName:string); var i,ii:integer;     polja:string; begin  table.active:=false;  with table do  begin    TableName:=FileName;    if BDETableSize>0 then    begin      FieldDefs.Clear;     for ii:=1 to BDETableSize do     begin      polja:=UpperCase(BDETableData[ii]);      for i:=1 to WordCNT(Polja) do      begin        if ExtractWords(i,1,Polja)='S' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftString,strtoint(Ext ractWords(i+2,1,Polja)),false);        end;        if ExtractWords(i,1,Polja)='I' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftInteger,0,false);        end;        if ExtractWords(i,1,Polja)='C' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftCurrency,0,false);        end;        if ExtractWords(i,1,Polja)='D' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftDate,0,false);        end;        if ExtractWords(i,1,Polja)='M' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftMemo,1,false);        end;        if ExtractWords(i,1,Polja)='+' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftAutoInc,0,false);        end;        if ExtractWords(i,1,Polja)='G' then        begin          FieldDefs.Add(ExtractWords(i+1,1,Polja),ftGraphic,0,false);        end;      end;     end;    end;    CreateTable;  end; end; initialization end. And usage of this: Code: [Select] //  S myfieldname size  = string //  I myfieldname         = integer //  C myfieldname        = currency //  D myfieldname        = date //  M myfieldname        = memo //  + myfieldname        = autoinc //  G myfieldname        = graphic   example:   BDEDataTable[1]:='S MyStringField 20';   BDEDataTable[2]:='M MyMemoField';   BDEDataSize:=2; // counter   CreateMyTable(TableDBF1,'c:\test\barko.db'); edit: added some missing functions and other things... edit2: it's work now as unit... ---------------------------------------------------------------------- Anonymous · 2006-04-27 19:56 To set the datatypes on openoffice you could try to do this: After each column name you could put a "," and then indicates the type. For example, if you want to set the datatype of the column "FIELD1" to character, you would put this on the first field: "FIELD1,C" Then, if you want to set the size of the column you put other "," and set the number of characters. For example, if the "FIELD1" column will have 7 characters you put "FIELD1,C,7" on the first field. ---------------------------------------------------------------------- Anonymous · 2006-04-27 19:58 Don´t forget to save as "dbf"... For integer types you use "N" instead "C".