How to get a TDBXTypes.BLOB field value?

Discussion of open issues, suggestions and bugs regarding usage of dbExpress drivers for InterBase & Firebird in Delphi and C++Builder
Post Reply
fabio_rbs
Posts: 9
Joined: Wed 27 Oct 2010 23:19

How to get a TDBXTypes.BLOB field value?

Post by fabio_rbs » Thu 26 May 2011 02:27

Hi,

I have a "BLOB SUB_TYPE 0" field that is returned as TDBXTypes.BLOB type, this field stores only text files. (I know it would be better if it were "SUB_TYPE 1", but it's "SUB_TYPE 0")

How can I get the field value and put it on a string ?

I tried with .GetAnsiString, .GetString, .GetWideString, and the error below occurs:
"TDBXTypes.BLOB value type cannot be accessed as TDBXTypes.WideString value type."

Please, see the code:

Code: Select all

procedure TFrmPrincipal.BitBtnTestClick(Sender: TObject);
var
  DBXTrn: TDBXTransaction;
  DBXCmd: TDBXCommand;
  DBXRdr: TDBXReader;
  sTxt: string;
begin
  sTxt := '';
  DBXTrn := DM.DBXCon.DBXConnection.BeginTransaction(TDBXIsolations.ReadCommitted);
  try
    DBXCmd := DM.DBXCon.DBXConnection.CreateCommand;
    try
      DBXCmd.Text := 'SELECT FIRST 1 XMLFILE FROM T_XMLTBL';
      DBXCmd.Prepare;
      DBXRdr := DBXCmd.ExecuteQuery;
      if DBXRdr.Next then
        sTxt := DBXRdr.Value['XMLFILE'].GetString;
      DBXRdr.Free;
    finally
      DBXCmd.Free;
    end;

    DM.DBXCon.DBXConnection.CommitFreeAndNil(DBXTrn);
  except
    DM.DBXCon.DBXConnection.RollBackFreeAndNil(DBXTrn);
    raise;
  end;
  ...
  ...
end;
I'm using Delphi 2010, Firebird 2.1.4 and DbxIda 2.70.28.

Thanks,

Fabio R. Bot da Silva.

AndreyZ

Post by AndreyZ » Thu 26 May 2011 11:00

Hello,

You can use the following code:

Code: Select all

procedure TFrmPrincipal.BitBtnTestClick(Sender: TObject); 
var 
  DBXTrn: TDBXTransaction; 
  DBXCmd: TDBXCommand; 
  DBXRdr: TDBXReader; 
  sTxt: string; 
  str: TStream;
  bytes: TBytes;
begin 
  sTxt := ''; 
  DBXTrn := DM.DBXCon.DBXConnection.BeginTransaction(TDBXIsolations.ReadCommitted); 
  try 
    DBXCmd := DM.DBXCon.DBXConnection.CreateCommand; 
    str := nil;
    try 
      DBXCmd.Text := 'SELECT FIRST 1 XMLFILE FROM T_XMLTBL'; 
      DBXCmd.Prepare; 
      DBXRdr := DBXCmd.ExecuteQuery; 
      if DBXRdr.Next then begin
        str := DBXRdr.Value['XMLFILE'].GetStream;
        SetLength(bytes, str.Size);
        str.ReadBuffer(bytes[0], str.Size);
        sTxt := StringOf(bytes);
      end;
      DBXRdr.Free; 
    finally 
      SetLength(bytes, 0);
      str.Free;
      DBXCmd.Free; 
    end; 

    DM.DBXCon.DBXConnection.CommitFreeAndNil(DBXTrn); 
  except 
    DM.DBXCon.DBXConnection.RollBackFreeAndNil(DBXTrn); 
    raise; 
  end; 
  ... 
  ... 
end;

fabio_rbs
Posts: 9
Joined: Wed 27 Oct 2010 23:19

Post by fabio_rbs » Thu 26 May 2011 13:39

Your code works fine.

Thanks a lot.

Fábio.

Post Reply