* * *

Author Topic: Field-Type TDbf ftFloat seems not workly correct  (Read 1396 times)

PascalGuru

  • Newbie
  • Posts: 3
Field-Type TDbf ftFloat seems not workly correct
« on: April 26, 2017, 10:46:39 pm »
  Field-Type  TDbf ftFloat  seems not workly correct.
  The result on a create Table with this Fieldtype,
  we get a Fieldtype  'Numerical' respective  'ftInteger
  and there a no possibility to perform decimal places in it.
 
    MyDbf.FieldDefs.Add('ZAHL_FLOAT', ftFloat, 10, true). 
 
  Now I have the Piece of Code:
 
  procedure TForm1.DBF_Datei_Neu(sender : TObject);
    begin
    MyDbf := TDbf.Create(nil);
    try
   
         (* Now we use an absolute Path to "DATA"-Directory *)
      MyDbf.FilePathFull := 'C:\PROJEKTE\PASCAL\DBA\DATA' + DirectorySeparator;
     
      (* Wir want to create an compatible DBASE4 Table *)
      MyDbf.TableLevel := 4;

      MyDbf.Exclusive := true;
      MyDbf.TableName := 'testpasc.dbf';
      (* For this wie need the Unit DB *)
      with MyDbf.FieldDefs do
        begin                                    // Here are my own-named DBASE-Fieldtypes
        Add('ID',         ftInteger,  3, true);  // N  3,0  NoIdx
        Add('NAME',       ftString,  20, true);  // C  2    NoIdx
       
        Add('ZAHL_BYTE',  ftSmallInt, 3, true);  // N  3    NoIdx
        Add('ZAHL_INT',   ftInteger,  6, true);  // N  6    NoIdx
        ...
       
        Add('ZAHL_FLOAT', ftFloat,   10, true);  // G 10,3  NoIdx 
        ... 
       
        end; (* of with MyDbf.FieldDefs *)
       
      MyDbf.CreateTable;
      MyDbf.Open;
      MyDbf.AddIndex('id', 'ID', [ixPrimary, ixUnique]);
 

      (* We fill the first Record *)
      MyDbf.Append;     // Create an Record
     
      MyDbf.FieldByName('ID').AsInteger        :=  1;
      MyDbf.FieldByName('NAME').AsString       := 'Jesus Christ';
      MyDbf.FieldByName('ZAHL_BYTE').AsInteger :=  255;
      MyDbf.FieldByName('ZAHL_INT').AsInteger  :=  32767;
     
      MyDbf.FieldByName('ZAHL_FLOAT').AsFloat  :=  3.14159;
      MyDbf.Post;       // Important for Store of the Record and Continuing 
      end; (* of with MyDbf.FieldDefs *)
   
    ...
    finally
      MyDbf.Free;
    end; (* of try-finally *)
  end; (* of procedure TForm1.DBF_Datei_Neu *)
     
  If we later load the Table in originally DBASE 4 , which we created with Component  TDbf ,
  we see that the field 'ZAHL_FLOAT' is always of Type 'Numerical', not of Type 'Gleitkomma'
  (it's a german DBASE, i don't know the correct english naming of this type).
  And we see in Field ZAHL_FLOAT only the Number  3 - without decimal places.
 
  What should I do?
  Is it really a bug in the Component TDbf ?

  Thanks for your help and answers,
  PascalGuru 

GAN

  • Jr. Member
  • **
  • Posts: 96
Re: Field-Type TDbf ftFloat seems not workly correct
« Reply #1 on: April 26, 2017, 11:41:32 pm »
From the only official guide of TDBF : https://sourceforge.net/projects/tdbf/files/TDbf%20documentation/TDbf%20documentation%201.1/

Code: Pascal  [Select]
  1. NewFieldDefs: TDbfFieldDefs ;
  2. NewFieldDef: TDbfFieldDef;
  3. Dbf1 : TDbf ;
  4. ...
  5. // create new field list
  6. NewFieldDefs := TDbfFieldDefs. Create ( Self );
  7. // assign current list
  8. NewFieldDefs. Assign ( Dbf1 . DbfFieldDefs);
  9. // assume first field is string , 20 wide , make larger to 40
  10. NewFieldDefs. Items [0]. Size := 40;
  11. // rename second field to ’ RENAMED ’
  12. NewFieldDefs. Items [1]. FieldName := ’ RENAMED ’;
  13. // add a float field
  14. NewFieldDef := NewFieldDefs. AddFieldDef;
  15. NewFieldDef. FieldName := ’ NEW_FLOAT ’;
  16. NewFieldDef. FieldType := ftFloat;
  17. NewFieldDef. Size := 10;
  18. NewFieldDef. Precision := 3;
  19. // restructure table and pack
  20. Dbf1 . Restructure( NewFieldDefs , true );
  21. // restructure table and not pack
  22. // Dbf1 . Restructure( NewFieldDefs , false );
  23. // free mem
  24. NewFieldDefs. Free ;

What you need:
Code: Pascal  [Select]
  1. NewFieldDef := NewFieldDefs. AddFieldDef;
  2. NewFieldDef. FieldName := ’ NEW_FLOAT ’;
  3. NewFieldDef. FieldType := ftFloat;
  4. NewFieldDef. Size := 10;
  5. NewFieldDef. Precision := 3; // <---------

Or you can try:
Code: Pascal  [Select]
  1.  Add('ZAHL_FLOAT', ftFloat,   10,  3, true);  // G 10,3  NoIdx  

Regards.
Lazarus 1.6 FPC 3.0.0 Linux Mint Mate 17.2 x86_64 GTK-2
Zeos 7.1.3 - Sqlite 3.8.2

Foro Lazarus en español http://forum.lazarus.freepascal.org/index.php/board,73.0.html

PascalGuru

  • Newbie
  • Posts: 3
Answer of: Field-Type TDbf ftFloat seems not workly correct
« Reply #2 on: May 03, 2017, 03:03:32 pm »
  Thank you very much for your Help.
  I had search and search and search again - but nothing find anywhere.
  Beside: The Link of the official Guide works - obviously the Website "Sourcefourge"
  are in most Cases does not work in Case of slower (and very slower) Connections.
 
  The official Guide is from 2004 and does not contain all relevant Points for Newbies.
  They should be stand in:
 
Code: Pascal  [Select]
  1.     uses db, dbf, dbf_fields, dbf_common;
  2.     var
  3.       NewFieldDefs : TDbfFieldDefs;
  4.       NewFieldDef  : TDbfFieldDef;
  5.       Dbf1         : TDbf;
  6.     begin
  7.     ...
  8.     // Create new Field-List
  9.     NewFieldDefs := TDbfFieldDefs.Create (Self);
  10.     // Assign current List
  11.     NewFieldDefs.Assign ( Dbf1.DbfFieldDefs);
  12.     // Assume first Field is string, 20 wide , make larger to 40
  13.     NewFieldDefs. Items [0].Size := 40;
  14.     // Rename second field to ’ RENAMED ’
  15.     NewFieldDefs. Items [1].FieldName := ’ RENAMED ’;
  16.     // Add a Float Field
  17.     NewFieldDef := NewFieldDefs.AddFieldDef;
  18.     NewFieldDef.FieldName := 'ZAHLFLOAT';
  19.     NewFieldDef.FieldType := ftFloat;
  20.     NewFieldDef.Size      := 10;
  21.     NewFieldDef.Precision := 3;
  22.     // Restructure Table and pack
  23.     Dbf1.Restructure(NewFieldDefs, true);
  24.     // Restructure Table and not pack
  25.     // Dbf1.Restructure(NewFieldDefs, false);
  26.     // Free mem
  27.     NewFieldDefs.Free;
  28.    
  Not all is good - the Command  Dbf1.Restructure  is not accepted.
  Which Unit for Restructure is needed ?
  I had try all: dbf_common, fpdbexport, fpdbfexport, fpfixedexport
  but nothing works!
  Like I said before - the Official Guide could be a Step better.
 
  The third suggestion with
Code: Pascal  [Select]
  1.     Dbf1.Add('ZAHL_FLOAT', ftFloat, 10, 3, true);
  2.  

 is not accepted without any more things. (This I had try as First.)
   
  The Single Apostroph on the Documentation is a wrong Sign -
  it doesn't accepted from LAZARUS/FREE-PASCAL/TURBO PASCAL/...  (only ' works!)
 
  Thank you for your Help again!
  PascalGuru
« Last Edit: May 03, 2017, 03:10:34 pm by PascalGuru »

PascalGuru

  • Newbie
  • Posts: 3
Re: Field-Type TDbf ftFloat seems not workly correct
« Reply #3 on: May 11, 2017, 04:49:06 pm »
Thank you, People, for your Helps.

The Code above is not correct.
  1. Instead of  "Restructure"  you must take the correct  "RestructureTable"
      In full Description is the correct Writing: 
        // Restructure Table and not pack
        Dbf1.RestructureTable(NewFieldDefs, false);
 
  2. After the Commands
       
Code: Pascal  [Select]
  1.         //Create new Field-List
  2.         Dbf1FieldDefs := tDbfFieldDefs.Create(Self);  
  3.         // Assign current List
  4.         Dbf1FieldDefs.Assign(Dbf1.DbfFieldDefs);  
  5.        
  6.         // Assume first Field is String, 20 wide, make larger to 40
  7.         Dbf1FieldDefs.Items[0].Size := 40;
  8.         // Rename second Field to 'ZAHLFLOAT'
  9.         Dbf1FieldDefs.Items[1].FieldName := 'ZAHLFLOAT';
  10.        
   
   The first Command behind  Assign I get an Runtime-Error:
   Projekt XXX has an Exception of "EConvertError"
   with the Message.  "Cannot assign a Nil to a TDbfFieldDefs"
   at Adress  43A177 

There are any Comments, Hints or Suggests ? 

Thank you for your Helping.
PascalGuru

Handoko

  • Hero Member
  • *****
  • Posts: 1732
  • My goal: build my own game engine using Lazarus
Re: Field-Type TDbf ftFloat seems not workly correct
« Reply #4 on: June 16, 2017, 05:40:07 pm »
I think you need this below before you can use the items because Items.Count = 0.

Code: Pascal  [Select]
  1.      NewFieldDef := Dbf1FieldDefs.AddFieldDef;

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus