Compare txt data with database data

Asked

Viewed 369 times

1

Follows the code:

procedure TfrmGrid.btnLoadClick(Sender: TObject);
var
 txt: TextFile;
 l,treg,treg2, coo, ccf: integer;
 valortxt, valorbd : double;
 lTemp, valor, dtcompratxt: String;
 dtcompra: TDateTime;
begin
DModuleGrid.ZQuery1.Close;
DModuleGrid.ZQuery1.SQL.Clear;
//Add tdcupant
//numcupom = coo
//Cupons
DModuleGrid.ZQuery1.SQL.Add('SELECT dtcompra, numnf, numcupom, ccf, valor FROM tdcupant');

DModuleGrid.ZQuery1.Open;

DModuleGrid.ClientDataSet1.SetProvider(DModuleGrid.DataSetProvider1);
DModuleGrid.ClientDataSet1.Open;

  l:= 1;
  treg:= 0;
  treg2:= 0;

  AssignFile(txt, frmSelection.FileListBox1.FileName);
  Reset(txt);
    while not eof(txt) do
    begin
     Readln(txt, lTemp);
     if (copy(lTemp, 1, 3) = 'E14') then
     begin
      inc(treg2);
      dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);
      TryStrToDateTime(dtcompratxt, dtcompra);

      //Comparando valores (Valor do Cupom)

      ShowMessage(DModuleGrid.ZQuery1.FieldByName('numcupom').AsString +' '+
        copy(lTemp,53, 6));
       if  (DModuleGrid.ZQuery1.FieldByName('numcupom').AsString = copy(lTemp,53, 6))
       and (DModuleGrid.ZQuery1.FieldByName('ccf').AsString = copy(lTemp,47,6))
       and (SameDate(DModuleGrid.ZQuery1.ParamByName('dtcompra').AsDate,
         dtcompra))
       then
       begin
        inc(treg);
        StringGrid1.RowCount := treg+1;
        inc(l);
        //Valor no BD
        StringGrid1.Cells[0,l] := DModuleGrid.ZQuery1.FieldByName('valor').AsString;
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        StringGrid1.Cells[1,l] := FloatToStr(StrToFloat(copy(lTemp, 109, 14))/100);
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;

        ShowMessage(FloatToStr(valortxt) + ' ' + FloatToStr(valorbd));
       end;
        //Diferença nos valores
       if not (StringGrid1.Cells[0,l] = StringGrid1.Cells[1,l]) then
        begin
            valor := FloatToStr(valorbd - valortxt);
         if (valor = '') then
           begin
            valor := IntToStr(0);
            StringGrid1.Cells[2,l] := valor;
           end
            else
              StringGrid1.Cells[2,l] := valor;
        end;
     end;
    end;
    ShowMessage('Existem '+ IntToStr(treg2) + ' linhas -E14');
    ShowMessage('Existem '+ IntToStr(treg) + ' linhas');
    CloseFile(txt);
end;

That code does not reach the second if, follows the code of the second if, which is also above:

dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);
      TryStrToDateTime(dtcompratxt, dtcompra);

      //Comparando valores (Valor do Cupom)

      ShowMessage(DModuleGrid.ZQuery1.FieldByName('numcupom').AsString +' '+
        copy(lTemp,53, 6));
       if  (DModuleGrid.ZQuery1.FieldByName('numcupom').AsString = copy(lTemp,53, 6))
       and (DModuleGrid.ZQuery1.FieldByName('ccf').AsString = copy(lTemp,47,6))
       and (SameDate(DModuleGrid.ZQuery1.ParamByName('dtcompra').AsDate,
         dtcompra))
       then
       begin
        inc(treg);
        StringGrid1.RowCount := treg+1;
        inc(l);
        //Valor no BD
        StringGrid1.Cells[0,l] := DModuleGrid.ZQuery1.FieldByName('valor').AsString;
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        StringGrid1.Cells[1,l] := FloatToStr(StrToFloat(copy(lTemp, 109, 14))/100);
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;

        ShowMessage(FloatToStr(valortxt) + ' ' + FloatToStr(valorbd));
       end;

This code cannot compare the data from one to the other.

What I’m doing wrong?

  • Ignore the ShowMessages.

2 answers

1


Follow the code I developed in response.

I hope it helps.

procedure TfrmGrid.btnLoadClick(Sender: TObject);
var
 txt: TextFile;
 treg, lreg: integer;
 valortxt, valorbd : double;
 lTemp, valor, dtcompratxt: String;
 dtcompra: TDateTime;
begin
lreg:= 1;
treg:= 0;

AssignFile(txt, frmSelection.FileListBox1.FileName);
Reset(txt);
while not eof(txt) do
begin
 Readln(txt, lTemp);
 if  (copy(lTemp, 1, 3) = 'E14') then
 begin
   dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);

   DModuleGrid.ZQuery1.Close;
   DModuleGrid.ZQuery1.SQL.Clear;
   DModuleGrid.ZQuery1.SQL.Add('SELECT dtcompra, numnf, numcupom, ccf, valor FROM tdcupant');
   //DModuleGrid.ZQuery1.SQL.Add('WHERE dtcompra = ' + dtcompratxt + ' AND numcupom = ' + copy(lTemp,53, 6) + ' AND numnf = ' + copy(lTemp, 4, 20) + ' AND ccf = '+ copy(lTemp,47,6) + ';');
   DModuleGrid.ZQuery1.Open;
   DModuleGrid.ClientDataSet1.SetProvider(DModuleGrid.DataSetProvider1);
   DModuleGrid.ClientDataSet1.Open;

   if not (DModuleGrid.ZQuery1.IsEmpty) then
   begin
     DModuleGrid.ZQuery1.First;
     while not DModuleGrid.ZQuery1.Eof do
     begin
       if (copy(lTemp,53, 6) = DModuleGrid.ZQuery1.FieldByName('numcupom').AsString)
       and (copy(lTemp,47,6) = DModuleGrid.ZQuery1.FieldByName('ccf').AsString)
       and (StrToDateTime(dtcompratxt) = DModuleGrid.ZQuery1.FieldByName('dtcompra').AsDateTime)
       then
       begin
        inc(lreg);
        //Valor no BD
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;
        //Diferença nos valores
        if (valorbd <> valortxt) then
        begin
          inc(treg);
         if (valor = '') then
           begin
            valor := IntToStr(0);
           end
            else
            valor := IntToStr(treg);
            Label1.Caption:='Divergências entre valores: '+ valor;
        end;

       end;
       DModuleGrid.ZQuery1.Next;
     end;
   end;
 end;
end;
ShowMessage('Existem '+ IntToStr(lreg) + ' linhas de Cupons(E14)');
CloseFile(txt);
CloseQuery;
end;
  • 2

    The lack of indentation in the code remains a big problem, because today, with 92 lines is easy to see where is what, but as systems grow and logic becomes complicated, bugs arise simply by lack of organization.

  • For example, if (valor = '') then seems to be a level to the left of the rest, so off the top Begin. Any carelessness and you close a loop in the wrong place.

  • you’re right, I have to work the identation part too! Obigado by the attention again @Filipe.Fonseca

  • If you have solved your question, please mark your answer as accepted, so that it no longer appears in unanswered questions.

0

Getting the indentation of your code (very important) I reached some conclusions. Follow:

First the code:

var
  txt: TextFile;
  l,treg,treg2, coo, ccf: integer;
  valortxt, valorbd : double;
  lTemp, valor, dtcompratxt: String;
  dtcompra: TDateTime;
begin
  DModuleGrid.ZQuery1.Close;
  DModuleGrid.ZQuery1.SQL.Clear;
  //Add tdcupant
  //numcupom = coo
  //Cupons
  DModuleGrid.ZQuery1.SQL.Add('SELECT dtcompra, numnf, numcupom, ccf, valor FROM tdcupant');
  DModuleGrid.ZQuery1.Open;
  DModuleGrid.ClientDataSet1.SetProvider(DModuleGrid.DataSetProvider1);
  DModuleGrid.ClientDataSet1.Open;
  l:= 1;
  treg:= 0;
  treg2:= 0;
  AssignFile(txt,frmSelection.FileListBox1.FileName);
  Reset(txt);
  while not eof(txt) do
  begin
    Readln(txt, lTemp);
    if (copy(lTemp, 1, 3) = 'E14') then
    begin
      inc(treg2);
      dtcompratxt := copy(lTemp,65,2)+'/'+copy(lTemp,63,2)+'/'+copy(lTemp,59,4);
      TryStrToDateTime(dtcompratxt, dtcompra);
      //Comparando valores (Valor do Cupom)
      ShowMessage(DModuleGrid.ZQuery1.FieldByName('numcupom').AsString +' '+
      copy(lTemp,53, 6));
      if  (DModuleGrid.ZQuery1.FieldByName('numcupom').AsString = copy(lTemp,53, 6))
           and (DModuleGrid.ZQuery1.FieldByName('ccf').AsString = copy(lTemp,47,6))
           and (SameDate(DModuleGrid.ZQuery1.ParamByName('dtcompra').AsDate,dtcompra)) then
      begin
        inc(treg);
        StringGrid1.RowCount := treg+1;
        inc(l);
        //Valor no BD
        StringGrid1.Cells[0,l] := DModuleGrid.ZQuery1.FieldByName('valor').AsString;
        valorbd := DModuleGrid.ZQuery1.FieldByName('valor').AsFloat;
        //Valor no TXT
        StringGrid1.Cells[1,l] := FloatToStr(StrToFloat(copy(lTemp, 109, 14))/100);
        valortxt := StrToFloat(copy(lTemp, 109, 14))/100;
        ShowMessage(FloatToStr(valortxt) + ' ' + FloatToStr(valorbd));
      end;
      //Diferença nos valores
      if not (StringGrid1.Cells[0,l] = StringGrid1.Cells[1,l]) then
      begin
        valor := FloatToStr(valorbd - valortxt);
        if (valor = '') then
        begin
          valor := IntToStr(0);
          StringGrid1.Cells[2,l] := valor;
        end
        else
          StringGrid1.Cells[2,l] := valor;
      end;
    end;
  end;
  ShowMessage('Existem '+ IntToStr(treg2) + ' linhas -E14');
  ShowMessage('Existem '+ IntToStr(treg) + ' linhas');
  CloseFile(txt);

As we see, the second and the third ifs are within the first. If the condition of the first is not satisfied, we will not reach the others.

As for what else may be being done wrong, it depends on your logic, the question becomes a little subjective.

  • 1

    I thought the same way too, and tried in a simpler way, @Filipe.Fonseca, I removed the stringGrids and just put a label with a counter to indicate me the Qtd of divergences between the valortxt and the valorbd, so I was able to get a basis on how to do the code. I posted the answer, I hope you like it.

Browser other questions tagged

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