txt p/ tabela paradox

Delphi

10/04/2003

Olá,

Já li várias mensagens mais nenhuma me ajudou a resolver meu problema, gostaria de saber como eu importo um arquivo txt delimitado por vírgulas para uma tabela db, se o arquivo fosse separacao por posições eu saberia fazer, mais ele está delimitado por vírgulas, já li mt a respeito, mais nada funcionou, será que alguém tem algum exemplo, de como fazer isso ou poderia me ajudar?

Se puderem enviar ao meu mail: sicad@uol.com.br

Obrigada Pela Atenção
Kris

Favor posta o título em minúsculas
Zoom


Anonymous

Anonymous

Curtidas 0

Respostas

Anonymous

Anonymous

10/04/2003

Vc vai usar normalmente o readln pra ler a linha do txt, mas quando for jogar no banco vc vai varrer a string em busca da virgula com a função POS, e dá um copy até a posição antes dessa virgula, depois dá outro POS na substring após a virgula e acha a segunda virgula, da outro copy e assim vai .......... Qualquer dúvida

leandrorenno@globo.com


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

10/04/2003

Kris,

Fiz um exemplo, lendo uma arquivo CSV (Texto separado por ;) e convertendo para uma estrutura XML (Sem cabeçalho, DTD ou Schema) para te servir de guia:

procedure TForm1.Button1Click(Sender: TObject);
var ReadFile: TextFile;
    LineData: String;
begin
  AssignFile(ReadFile,´C:\ARQUIVO.CSV´);
  Reset(ReadFile);
  while not Eof(ReadFile) do
  begin
    ReadLn(ReadFile,LineData);
    mmXML.Lines.Add(XMLConvert(LineData));
  end;
  CloseFile(ReadFile);
end;

function TForm1.XMLConvert(Linha: String): String;
var nI: Integer;
    nPos: Integer;
    nLast: Integer;
    nCol: Integer;
begin
  result  := ´´;
  nCol    := 0;

  For nI:=1 to Length(Linha) do
  begin
    nPos := Pos(´;´,Linha);
    if nPos>1 then
    begin
      Inc(nCol);
      result := result + ´<COL´+IntToStr(nCol)+´>´ + Copy(Linha, nLast+1, (nPos-nLast)-1) + ´</COL´+IntToStr(nCol)+´>´;
    end;
    if nPos>0 then Linha[nPos] := ´|´;
    nLast := nPos;
  end;
end;



GOSTEI 0
Anonymous

Anonymous

10/04/2003

Segue o DFM para facilitar a reprodução do exemplo:

  object Button1: TButton
    Left = 62
    Top = 50
    Width = 75
    Height = 25
    Caption = ´Button1´
    TabOrder = 0
    OnClick = Button1Click
  end
  object mmXML: TMemo
    Left = 222
    Top = 0
    Width = 466
    Height = 453
    Align = alRight
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = ´Courier New´
    Font.Style = []
    ParentFont = False
    TabOrder = 1
  end



GOSTEI 0
Aroldo Zanela

Aroldo Zanela

10/04/2003

Fix:
function TForm1.XMLConvert(Linha: String): String;
var nI: Integer;
    nPos: Integer;
    nLast: Integer;
    nCol: Integer;
begin
  result  := ´´;
  nCol    := 0;

  For nI:=1 to Length(Linha) do
  begin
    nPos := Pos(´;´,Linha);
    if nPos>1 then
    begin
      Inc(nCol);
      result := result + ´<COL´+IntToStr(nCol)+´>´ + Copy(Linha, nLast+1, (nPos-nLast)-1) + ´</COL´+IntToStr(nCol)+´>´;
    end;
    if (nPos=0) and (nPos<>nLast) then
    begin
      Inc(nCol);
      result := result + ´<COL´+IntToStr(nCol)+´>´ + Copy(Linha, nLast+1, (Length(Linha)-nLast)-1) + ´</COL´+IntToStr(nCol)+´>´;
    end;
    if nPos>0 then Linha[nPos] := ´|´;
    nLast := nPos;
  end;
end;



GOSTEI 0
Sicad

Sicad

10/04/2003

Muito Obrigada Leandro e Aroldo pela ajuda.

Consegui resolver meu problema com o exemplo que vc mandou Aroldo. :lol:


Mais uma vez obrigada.
Kris


GOSTEI 0
POSTAR