save in the txt file the result of the sql query in firebird using Delphi

1

I need to save the result of a query inside the txt file by separating by a delimiter, I am extremely new to delphi and firebird, so any help is welcome

code below:

procedure TForm1.btPesquisarClick(Sender: TObject);
var arquivo: TextFile;
var dados: string;
var I: Integer;
begin

  if EdLoja.Text = '' then
  begin

  IBQuery1.Close;
  IBQuery1.SQL.Clear;
  IBQuery1.SQL.Add('select COUNT(CDLOJA) from lojas');
  IBQuery1.Open;

  while not IBQuery1.eof do
    begin

IBQuery2.Close;
IBQuery2.SQL.Clear;

IBQuery2.SQL.Add('select * from lojas where CDLOJA = '+(EdLoja.Text)+'');
IBQuery2.SQL.Add('and p.cdloja = '+(IBQuery1.Eof)+'');
IBQuery2.SQL.Add('and p.dtlancamento >= '+ FormatdateTime('mm"-"dd"-"yyyy',StrToDate(EdDataIni.Text)) + '');
IBQuery2.SQL.Add('and p.dtlancamento <= '+ FormatdateTime('mm"-"dd"-"yyyy',StrToDate(EdDataFim.Text)) + '');

IBQuery2.Open;
IBQuery2.First;
AssignFile(arquivo, 'c:\'+IBQuery2.FieldByName('CDLOJA').AsString+'Vencimento.txt');
Rewrite(arquivo);


dados := IBQuery2.FieldByName('CDLOJA').AsString + ';' + IBQuery2.FieldByName('CDBAIRRO').AsString;
WriteLn(arquivo, dados);
IBQuery2.Next;

CloseFile(arquivo);

end;

  ShowMessage('ARQUIVOS GERADO COM SUCESSO!');

  end;

end;

end.
    
asked by anonymous 13.12.2018 / 15:15

1 answer

1

In this part you need an adjustment:

 while not IBQuery2.eof do

 begin
  WriteLn(arquivo);
  IBQuery2.Next;
 end;

To:

while not IBQuery2.eof do
begin
  vTexto := IBQuery2.FieldByName('NOME_CAMPO_1').AsString + ';' + IBQuery2.FieldByName('NOME_CAMPO_2').AsString
  WriteLn(arquivo, vTexto);
  IBQuery2.Next;
end;
    
13.12.2018 / 16:09