Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread Help writing blob field
Thu, Mar 24 2011 10:43 PMPermanent Link

quirjo

I have the following procedure to modify a field (TotalPrecio) data blob but it does not, I'm doing wrong, help:

procedure TFPostCambios.Actualiza_Costo;
 type
   RegUnPrecio = Record
   PorcUtil,PorcUtilEx : Boolean;
   Utilidad,UtilidadEx,SinImpuesto,MtoImpuesto1,MtoImpuesto2 ,
   TotalPrecio ,TotalPrecioEx: Currency;
   TipoRound : Byte;
 end;
 RegCostos = Record
   CodeCompra : String[50];
   VImpuesto1 ,
   VImpuesto2 : Boolean;
   CostoAnteriorBs ,
   CostoAnteriorEx ,
   CostoActualBs ,
   CostoActualEx ,
   CostoPromedioBs ,
   CostoPromedioEx ,
   MImpuesto1 ,
   MImpuesto2 : Currency;
   PorcentImp1 ,
   Exento1 ,
   PorcentImp2 ,
   Exento2 : Boolean;
   FechaVencimiento: TDateTime;
   NumeroDeLote : String[50];
   Precios : Array[1..6] of RegUnPrecio;
 end;
 var
   registro:regcostos;
   Stream1 : TDBISAMBLOBstream;
begin
 with datos.costos do
   begin
     Open;
     SetKey;
     Fields[0].AsString := 'B';
     Fields[1].AsString := Codigo;
     if  GotoKey then
       begin
         edit;
         try
           Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')),bmWrite);
           Stream1.Read(registro,Sizeof(registro));
           registro.Precios[1].TotalPrecio:=precio;   //
           Stream1.Write(registro,Sizeof(registro)); //Write la variable blob en mi record
         finally
           Stream1.Free;
         end;
         post;
       end
     else
       begin
         //Agrego si no existe
         Append;
         FieldByName('FX_TIPO').AsString:='B';
         FieldByName('FX_CODIGO').AsString:=Codigo;
         try
           Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')),bmwrite);
           registro.CostoPromedioBs:=Costo;
           registro.Precios[1].TotalPrecio:=Precio;
           Stream1.Write(registro,Sizeof(registro));// Escribo la variable blob en mi record
         finally
           Stream1.Free;
         end;
         Post;
       end;
     Close;

  end;
end;
Fri, Mar 25 2011 2:40 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate

Hello

I don't have an answer, but I have some questions Smiley

1. What version are you using?

2. When you say it doesn't modify the field, what do you mean? Can you be more specific?
Are you getting an error message, or it simply has no effect?

3. Are you sure that Fields[0] and  Fields[1] are the same fields as 'FX_TIPO' and 'FX_CODIGO' ?
You should use FieldByName instead of Fields[0] Fields[1], unless you have a good reason not to.

4. This is not the solution for your issue, but anyway, the try.. except block protecting the stream is not correct.
The creation on the stream should be out of the block, like this:

Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')), bmWrite);
try
 ...
 ...
except
  Srtream1.Free;
end;


--
Fernando Dias
[Team Elevate]
Fri, Mar 25 2011 2:43 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate


Correction:

Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')), bmWrite);
try
 ...
 ...
FINALLY
  Srtream1.Free;
end;


--
Fernando Dias
[Team Elevate]
Sun, Mar 27 2011 8:54 PMPermanent Link

quirjo

Fernando Dias wrote:


Correction:

Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')), bmWrite);
try
 ...
 ...
FINALLY
  Srtream1.Free;
end;


--
Fernando Dias

I made the correction that you told me, but did not solve the problem. Using DBISAM version 4.28 of when I say that does not change the Fields I mean, not rewritten with the new value given, I get no error message but the rest of the record fields are damaged. I am sure that the fields [0] and y fields [1] are right on the search and go do the routine but the problem is not that but the above: Do not adjust the field:

registro.CostoPromedioBs: = costo;
registro.Precios [1]. TotalPrecio: = Precio;

to which I refer. Excuse my bad English, Thanks
[Team Elevate]
Mon, Mar 28 2011 12:36 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate


The Try/Except fix was not intended to solve your issue, it was a secondary issue.

You said the rest of the fields are damaged... do you mean the record is corrupt?
Version 4.28 addressed many issues related to BLOB fields, including some that could cause blob corruption.
Can you upgrade to a newer version, like 4.30 ?

--
Fernando Dias
[Team Elevate]
Mon, Mar 28 2011 12:50 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate


I just noticed another detail:


You are opening the stream for write (bmWrite), and it should be for Read/Write (bmReadWrite)

Your code is:
          edit;
          try
            Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')),bmWrite);
            Stream1.Read(registro,Sizeof(registro));
            registro.Precios[1].TotalPrecio:=precio;   //
            Stream1.Write(registro,Sizeof(registro)); //Write la variable blob en mi record

It should be:
          edit;
          try
            Stream1 := TDBISAMBlobStream.Create(TBlobField(FieldByName('FX_COSTOS')),bmReadWrite);



Fernando Dias
[Team Elevate]
Mon, Mar 28 2011 1:05 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate


Heh, another important thing:
When you do the first Read, the current position in the stream moves forward,
so when you then write to the stream, you are not writing at the correct position.
You have to reposition the stream at the beginning, like this:
 
  Stream1.Read(registro, Sizeof(registro));
  registro.Precios[1].TotalPrecio:=precio;
  Stream1.Seek(0, soFromBeginning);
  Stream1.Write(registro,Sizeof(registro));


--
Fernando Dias
[Team Elevate]
Mon, Mar 28 2011 5:55 PMPermanent Link

quirjo

Thanks Fernando, and had placed bmReadWrite, but with this last bit of code solved the problem now is working as it should be
 
  Stream1.Read(registro, Sizeof(registro));
  registro.Precios[1].TotalPrecio:=precio;
  Stream1.Seek(0, soFromBeginning);
  Stream1.Write(registro,Sizeof(registro));

Thank you very much
Mon, Mar 28 2011 7:02 PMPermanent Link

Fernando Dias

Team Elevate Team Elevate


A last recommendation:
You have been writing data in the blob fields in a wrong position; you should now 'clean' the blobs.

--
Fernando Dias
[Team Elevate]
Image