I cannot write data to a txt file

Asked

Viewed 45 times

-1

Code:

qr.ParamByName('mes').AsString:= edmes.Text;
qr.ParamByName('ano').AsString:= edano.Text;
qr.Active:= true;
qr.First;
try
  AssignFile(arq, 'relatorio.txt');
  Rewrite(arq);

  showmessage(inttostr(qr.RecordCount));

  while not qr.Eof do
  begin
    writeln(arq, qr.FieldValues['dt_ficha'] + ';' + qr.FieldValues['unidade'] + ';' + qr.FieldValues['profissional'] + ';' + qr.FieldValues['count']);
    qr.Next;
  end;

  CloseFile(arq);
  ShowMessage('Arquivo criado com sucesso!');
except
end;

The qr component runs correctly, showmessage tells me it has 270 lines, but the while command runs only once (one line only).

1 answer

1


Try this way

qr.ParamByName('mes').AsString:= edmes.Text;
qr.ParamByName('ano').AsString:= edano.Text;
qr.Open;

try
  AssignFile(arq, 'relatorio.txt');
  Rewrite(arq);

  showmessage(inttostr(qr.RecordCount));
  qr.First;
  while(not(qr.Eof))do
  begin
    writeln(arq, DateTimeToStr(qr.FieldValues['dt_ficha']) + ';' + qr.FieldValues['unidade'] + ';' + qr.FieldValues['profissional'] + ';' + IntToStr(qr.FieldValues['count']));
    qr.Next;
  end;

  CloseFile(arq);
  ShowMessage('Arquivo criado com sucesso!');
except
  On E: Exception do
    ShowMessage(E.message);
end;
  • didn’t work. worse this procedure is simple and I’m not understanding which error...

  • 1

    What is your version of Delphi? Have you programmed anything at any of the query events? Before writeln put this ShowMessage(IntToStr(qr.recno)) and see if the first time you enter you are in Recno 0 or 1

  • using recno appears 1

  • my version is rad studio 10.2

  • I am using zeos to connect, the data is listed on the grid on the screen normally, only at the time of going through the file it does not do

  • 1

    Check my answer again, change the exception As I showed, it is to be displayed the error, I believe it is giving error when saving your text file, so it passes only once. And one more thing... If you are in version 10.2 which is the most current, use the components FireDAC instead of Zeos, they’re much better

  • That’s right, thanks. is asking to convert a variable of type date (dt_plug) to string

  • I’m using zeos that is free (my version of Delphi is a free, because I didn’t want to get pirate version), is it possible to install Firedac in it? can change your answer to me to give your points

  • 1

    Edited response. Following logic, possibly its field count would give problem also for being whole (I believe). And in the case of free Delphi, so beauty, fireDAC is more for paid version even.

  • That’s right, I’ve changed the field count also and I’m already using the system. hug ^^

Show 5 more comments

Browser other questions tagged

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