Board index » delphi » Getting dBase IV-database structure

Getting dBase IV-database structure

Hi,

in my program I need to get informations about the structure
of a dBase IV-database.
Especially the size of an integer-field. When I say the
size, I don't mean the size in byte. I need the size as I
can create in dBase. In dBase I create a datafield with
name, Typ (in this case integer) and size. Size means here
the number of digits and not the number of bytes.

I tried to solve this problem, with the Item-property of
TFieldDefs or the Fields-property of TFieldList. But in
TDBGrid I can only input 3 digits if there is the fieldsize
in dBase 3 digits....
Where can I find this information.

My next problem is that in the database I have to use, the
first field is an integer field and is the primary index
field. If I read TFieldType, I get only for this integer
field ftFloat????????????? I looked with dBase and the
Database Desktop in the structure and there is everywhere
the symbol for an intger field.

I can only use dBase-databases, because still we use some
old dBase-programms. I write my program with Delphi 4.

thanks in progress,

Stefan Michel

WIGE-MIC Leipzig
The Electronic World Of Sport

 

Re:Getting dBase IV-database structure


On Mon, 18 Oct 1999 18:42:18 +0200, Stefan Michel

Quote
<miche...@wige-mic.de> wrote:
>in my program I need to get informations about the structure
>of a dBase IV-database.
>Especially the size of an integer-field. When I say the
>size, I don't mean the size in byte. I need the size as I
>can create in dBase. In dBase I create a datafield with
>name, Typ (in this case integer) and size. Size means here
>the number of digits and not the number of bytes.

You can get this information using direct BDE API calls.  Something
like this:

// Gets low level BDE information of a field
function GetFieldDesc(Table: TTable; FieldName: String): PFldDesc;
var
   CursorProps: CurProps;
   ItrDsc: Integer;
begin
   Result := nil;
   if FFieldDescs = nil then
   begin
      DbiGetCursorProps(Table.Handle, CursorProps);
      FFieldDescCount := CursorProps.iFields;
      FFieldDescs := AllocMem(FFieldDescCount * SizeOf(FLDDesc));
      Check(DbiGetFieldDescs(Table.Handle, PFldDesc(FFieldDescs)));
   end;
   for ItrDsc := 0 to FFieldDescCount - 1 do
      if StrIComp(FFieldDescs^[ItrDsc].szName,
             PChar(FieldName)) = 0 then
      begin
         Result := @FFieldDescs^[ItrDsc];
         Break;
      end;
end;

Now you can use:

with GetFieldDesc(Table1, 'MyNumField') do
begin
   FldLen := iUnits1;
   FldDec := iUnits2;
end;

Hope this helps,

Jan

Re:Getting dBase IV-database structure


Quote
>You can get this information using direct BDE API calls.  Something
>like this:

I took this function from a class, but apparently it isn't very well
adapted to a standalone function (FFieldDescs is undefined and will
never be released as it is now).

Here's an improved version:

type
  PFieldDescList = ^TFieldDescList;
  TFieldDescList = array[0..1023] of FLDDesc;

procedure GetFieldAttrib(Field: TField; var Len, Dec: Integer);
var
   CursorProps: CurProps;
   FieldDescs: PFieldDescList;
   FieldDescCount, ItrDsc: Integer;
begin
   Len := 0;
   Dec := 0;
   DbiGetCursorProps((Field.DataSet as TTable).Handle, CursorProps);
   FieldDescCount := CursorProps.iFields;
   FieldDescs := AllocMem(FieldDescCount * SizeOf(FLDDesc));
   try
      Check(DbiGetFieldDescs((Field.DataSet as TTable).Handle,
        PFldDesc(FieldDescs)));
      for ItrDsc := 0 to FieldDescCount - 1 do
         if StrIComp(FieldDescs^[ItrDsc].szName,
                PChar(Field.FieldName)) = 0 then
         begin
            Len := FieldDescs^[ItrDsc].iUnits1;
            Dec := FieldDescs^[ItrDsc].iUnits2;
            Break;
         end;
   finally
      FreeMem(FieldDescs, FieldDescCount * SizeOf(FLDDesc));
   end;
end;

Jan

Re:Getting dBase IV-database structure


Hi Jan,

thanks for your help. It works good with the new lines of
code.

There is only one question left. Can I change the values by
using the API functions and setting a new value?

greetings

Stefan Michel
WIGE-MIC Leipzig
The Electronic World Of Sport

Re:Getting dBase IV-database structure


On Wed, 20 Oct 1999 10:19:31 +0200, Stefan Michel

Quote
<miche...@wige-mic.de> wrote:
>There is only one question left. Can I change the values by
>using the API functions and setting a new value?

When the question is "how can I restructure a DBF file?", the easiest
answer is through local SQL ALTER TABLE (see LocalSQL.HLP for
examples).

Another approach that I often use is to recreate the table with the
desired new structure and then batchmove the data.  This has the
advantage of performing a PACK at the same time.

HTH,

Jan

Other Threads