Recent

Author Topic: dbf data read/ write to table  (Read 8222 times)

draggon

  • New Member
  • *
  • Posts: 41
dbf data read/ write to table
« on: February 29, 2016, 10:10:46 am »
not clear how to read/ write data to the *.dbf table from within application. Makes sense ODBC connection? Enable sODBC using SQL?

Thank you

Thaddy

  • Hero Member
  • *****
  • Posts: 14200
  • Probably until I exterminate Putin.
Re: dbf data read/ write to table
« Reply #1 on: February 29, 2016, 10:26:42 am »
not clear how to read/ write data to the *.dbf table from within application. Makes sense ODBC connection? Enable sODBC using SQL?

Thank you

You don't need ODBC. There are Lazarus native controls for dBase files. And SQL is inherently not supported for the dBase format.
 
Specialize a type, not a var.

draggon

  • New Member
  • *
  • Posts: 41
Re: dbf data read/ write to table
« Reply #2 on: February 29, 2016, 12:26:14 pm »
example would be highly appreciated

draggon

  • New Member
  • *
  • Posts: 41
Re: dbf data read/ write to table
« Reply #3 on: February 29, 2016, 01:43:50 pm »
I mean how to read/ write particular record/ field in the record.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: dbf data read/ write to table
« Reply #4 on: February 29, 2016, 01:51:40 pm »
Try this and see if that helps you.

draggon

  • New Member
  • *
  • Posts: 41
Re: dbf data read/ write to table
« Reply #5 on: February 29, 2016, 02:41:14 pm »
thanks Molly, I have this, unfortunately it does not solve my question. Good example would be to move data from an array to db table.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: dbf data read/ write to table
« Reply #6 on: February 29, 2016, 04:57:03 pm »
.. Good example would be to move data from an array to db table.
No idea, why one would want to do that. Which is probably why there isn't such an example.

Consider this a bad written example:
Code: [Select]
program test2;

{$MODE OBJFPC}{$H+}

uses
  Heaptrc, Dbf, db, Dbf_Common, SysUtils; 


Const
  DBFileName = 'mydata.dbf';
 
var
  MyDbf     : TDbf;
  DataArray : Array of SmallInt;


Procedure Create_Some_Array_Data;
var
  i : Integer;
begin
  SetLength(DataArray, 1000000);
  for i := Low(DataArray) to High(dataArray) do
    DataArray[i] := Random(65536) + 1 - (65536 shr 2);
end;


procedure Do_DBF_Stuff;
var
  i : Integer;
begin
  MyDbf := TDbf.Create(nil);

  Try
    if Not FileExists(DBFileName) then
    begin
      Writeln('creating DB');
      MyDbf.TableLevel := 7;
      MyDbf.Exclusive := True;
      MyDbf.TableName := DBFileName;
      with MyDbf.FieldDefs do
      begin
        Add('Number', ftinteger, 4, true);   
      End;
      MyDbf.CreateTable;
    end
    else
    begin
      MyDbf.TableLevel := 7;
      MyDbf.Exclusive := True;
      MyDbf.TableName := DBFileName;
    end;


    // Write some array to the table
    try
      Writeln('Adding Data');

      MyDBF.Open;
      MyDBF.Last;
      for i := Low(DataArray) to High(dataArray) do
      begin
        MyDBF.insert;
        MyDBF.Fields.Fields[0].Value:= DataArray[i];
        MyDBF.Post;
      end;
    finally
      MyDBF.Close;
    end;


    // Read Some Array from the table   
    try   
      Writeln('Reading Data');

      MyDBF.Open;
     
      i := MyDBF.ExactRecordCount;
      SetLength(DataArray, i);
      i := Low(DataArray);

      MyDBF.First;
      while not MyDBF.EOF do
      begin
        DataArray[i] := MyDBF.Fields[0].AsInteger;
        MyDBF.Next;
        inc(i);
      end;   
    finally
      MyDBF.Close;
    end;

  finally
    MyDbf.Free;
  end;
end;


Begin
  WriteLn('Creating array Data');
  Create_Some_Array_Data;

  WriteLn('Perform some DBF action -> Get yourself a cuppa tea');
  Do_DBF_Stuff;

  WriteLn('Done');
end.

Strange, as i was able to find everything i used here from the wiki (except for one minor thing, but nothing a google search couldn't figure out).  So.... what's your excuse ?  :-*

 

TinyPortal © 2005-2018