Problem for Blob

alexmorel

Member
I create programming Delphi 6

Code:
var LOB:TLobLocator;
Begin

With OracleQuery do
  Begin
    SQL.Clear;
    SQL.Add('INSERT INTO imagesB VALUES(44, :image)');
    LOB:=TLobLocator.Create(OracleSession1, otBLOB);
    DeclareVariable('image', otBLOB);
    SetComplexVariable('image', LOB);
    Execute;
    LOB.LoadFromFile(OpenPictureDialog1.FileName);
    LOB.Free;
    OracleSession1.Commit;
  End;
  End;
I can executer but an error: TLOBLocator: Invalid handle. !!

The files which I cross are of type *.jpg and *.bmp.

My Table :
ImagesB(
id_image number(6),
image BLOB);
 
There are 2 ways to fix this. The first works on any Oracle8+ database:

Code:
var LOB:TLobLocator;
Begin
  With OracleQuery do
  Begin
    SQL.Clear;
    SQL.Add('INSERT INTO imagesB VALUES(44, empty_blob()) returning image into :image');
    LOB:=TLobLocator.Create(OracleSession1, otBLOB);
    DeclareVariable('image', otBLOB);
    SetComplexVariable('image', LOB);
    Execute;
    LOB.LoadFromFile(OpenPictureDialog1.FileName);
    LOB.Free;
    OracleSession1.Commit;
  End;
End;
Note the returning into clause that returns an initialized BLOB to the client. The second method only works on Oracle8i and later:

Code:
var LOB:TLobLocator;
Begin
  With OracleQuery do
  Begin
    SQL.Clear;
    SQL.Add('INSERT INTO imagesB VALUES(44, :image)');
    LOB:=TLobLocator.CreateTemporary(OracleSession1, otBLOB, True);
    LOB.LoadFromFile(OpenPictureDialog1.FileName);
    DeclareVariable('image', otBLOB);
    SetComplexVariable('image', LOB);
    Execute;
    LOB.Free;
    OracleSession1.Commit;
  End;
End;
Note that this example uses a temporary BLOB, and writes the data before executing the insert statement.
 
Back
Top