Save any database in a text delimiter file  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 20/01/00

Tip accessed 563 times

 


Due to the petition of an user that he requested me something like that for Paradox tables:
Although..., thanks to a DataSource... it is good for any dataset to which is associate.

The function generates a text file, in which records all the registers of the database associated to the DataSource, generating one line for each register, and separating the fields con a given delimitador.


NOTE: The function, it jumps the binary fields (memo, images, blobs, etc).


 procedure TForm1.Button1Click(Sender: TObject);

   procedure GrabaDelimitado( Datos:TDatasource;
                              Fichero: string;
                              Delimitador:char);
   var
      f      : textfile;
      n      : integer;
      Linea  : string;
   begin
     AssignFile(f,Fichero);
     Rewrite(f);

     With Datos.DataSet do
     begin
       DisableControls;
       {Vamos al primer registro}
       {First record}
       First;
       {Vamos registro a registro, hasta el final}
       {From first to last, one by one}
       While Not Eof do
       begin
         Linea:='';
         {Recorremos todos los campos del registro}
         {all field of the record}
         for n:=0 to FieldCount-1 do
         begin
           If (Fields[n] is TBlobField) or
              (Fields[n] is TGraphicField) or
              (Fields[n] is TMemoField) or
              (Fields[n].IsNull)
           then
             Linea:=Linea+Delimitador
           else
             Linea:=Linea+Delimitador+Fields[n].AsString;
         end; {Del For}
         Linea:=Copy(Linea,2,Length(Linea)-1);
         Memo1.Lines.Add(Linea);
         writeln(f,Linea);
         Next;
       end; {Del While}
       EnableControls;
     end; {Del With}
     CloseFile(f);
   end;

 begin
   GrabaDelimitado(DataSource1,'c:\kk\Texto.txt',',');
 end;




Things to keep in mind: if in a text field, you have some character the same as the delimiter that you have chosen... you will have problems.
For example, if your you use a comma ', ' of delimitador, and in some text field you have some registration that have some comma in the text... because the result will be not well delimited.
Although I suppose that it won't be you problem to use another delimiter... or to use a delimiter of several characters, changing the parameter char of the function for string.