save in txt the result of the sql query in Firebird using Delphi

Asked

Viewed 661 times

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.
  • Ezequiel, you have some problems in your question and one of them is code formatting, for this edit and use Ctrl+K. It would be nice to improve, to avoid closing and always try to ask questions that will be useful to others in the community, see [Ask].

1 answer

1

This part needs an adjustment:

 while not IBQuery2.eof do

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

for:

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;
  • Good afternoon junior, next, that part I saw worked super well, vText replaces by data, string right?

  • 1

    However it goes into infinite loop because of Query1, what I want to do is the following, it makes the first select, counts how many stores have, and then in the next select putting the code of each store, it brings one select at a time and saves in separate files one loop at a time, searching the store pointed according to the loop of Query1 and already saved in aquivoloja1, next archive2 and etc until the end of the loop

  • Observe before the CloseFile(arquivo); you put IBQuery2.Next; and then did not send the IBQuery1 proceed, it will stay there forever, so remove the IBQuery2.Next; and put IBQuery1.Next after the CloseFile

Browser other questions tagged

You are not signed in. Login or sign up in order to post.