Board index » delphi » newbie question about putting file into a db table

newbie question about putting file into a db table

I have a tab delimited file that I need to turn into a temporary table so I
can compare this table's fields to a table in a SQL database.

I'm use to VB and ADO where I could easily build a table in memory right
from the file.

Is there an easy way to do this in Delphi(5) Pro version? The table has the
potential to be very large. ie 2-300,000 records.

 

Re:newbie question about putting file into a db table


If you are using ADO components (ADOExpress, Delphi 5) or a MS Access
database (using BDE MSACCESS or ODBC) you can do what you always have done.
If you are using another database type you have to manually import the data,
e.g.:

var
  F: TextFile;
  Line: String;
  Fields: TStrings;
  i, n: Integer;
begin
  AssignFile(F, 'filename.csv');
  FileMode := 0;
  Reset(F);
  try
    Table1.EmptyTable;
    Fields := TStringList.Create;
    try
      while not EOF(F) do
      begin
        ReadLn(F, Line);
        Fields.CommaText := Line;
        n := Table1.FieldCount;
        if n > Fields.Count then
          n := Fields.Count;
        Table1.Edit;
        try
          for i := 0 to n - 1 do
            Table1.Fields[i].AsString := Fields[i];
          Table1.Post;
        except
          Table1.Cancel;
        end;
      end;
    finally
      Fields.Free;
    end;
  finally
    CloseFile(F);
  end;
end;

"rut" <r...@eal.com> schreef in bericht
news:57Yk5.1216$IH.40928@newsread2.prod.itd.earthlink.net...

Quote
> I have a tab delimited file that I need to turn into a temporary table so
I
> can compare this table's fields to a table in a SQL database.

> I'm use to VB and ADO where I could easily build a table in memory right
> from the file.

> Is there an easy way to do this in Delphi(5) Pro version? The table has
the
> potential to be very large. ie 2-300,000 records.

Re:newbie question about putting file into a db table


Rut,

The easiest way is loading the file using the fileopen,readln methode. Just
walki through the string, looking for the tab char (chr(9) if I'm correct).
It would look something like this

Var T : TextFile;
        S2,
        S : String;
        TmI : Integer;
  begin
     AssignFile(T,FileName);
     Reset(T);
     While NOT EOF(T) do
          begin
            ReadLN(T,S);
            //Line read from file into S
            S2 := ''; //Make sure S2 is empty
            For TmI := 1 to Length(S) do //Walk through string
               begin
                   If S[TmI] <> Chr(9) then
                     S2 := S2 + S[TmI]
                  else
                     begin
                         //Now S2 holds one field as a string
                         //Use S2 to get the field
                         //And then empty the S2 variable for the next field
                         S2 := '';
                     end;
               end;
          end;
     CloseFile(T);
 end;

Good Luck

Laurens Lueks

rut heeft geschreven in bericht
<57Yk5.1216$IH.40...@newsread2.prod.itd.earthlink.net>...

Quote
>I have a tab delimited file that I need to turn into a temporary table so I
>can compare this table's fields to a table in a SQL database.

>I'm use to VB and ADO where I could easily build a table in memory right
>from the file.

>Is there an easy way to do this in Delphi(5) Pro version? The table has the
>potential to be very large. ie 2-300,000 records.

Other Threads