Recent

Author Topic: creating TDBF tables  (Read 7063 times)

ilniko

  • New Member
  • *
  • Posts: 18
creating TDBF tables
« on: March 01, 2006, 08:22:21 pm »
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

  • New Member
  • *
  • Posts: 45
RE: creating TDBF tables
« Reply #1 on: March 01, 2006, 08:50:57 pm »
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(ExtractWords(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

  • Guest
RE: creating TDBF tables
« Reply #2 on: April 27, 2006, 09:56:46 pm »
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

  • Guest
RE: creating TDBF tables
« Reply #3 on: April 27, 2006, 09:58:41 pm »
Don´t forget to save as "dbf"... For integer types you use "N" instead "C".

 

TinyPortal © 2005-2018