Recent

Author Topic: IBX and BLOB update  (Read 3656 times)

sydenis

  • New Member
  • *
  • Posts: 31
IBX and BLOB update
« on: June 30, 2021, 07:51:39 pm »
Ubuntu 20.04 + Lazarus 2.0.12
I use the IBSQL component from ibx4lazarus2. 4-0 to access the Firebird 3 database
I need to write and read a BLOB field from my database.

There are no problems with reading from db:
Code: Pascal  [Select][+][-]
  1. IBSQL1.FieldByName('blob').AsBlob.SaveToFile(MyFile);
I get the desired file.

But writing to db does not work:
Code: Pascal  [Select][+][-]
  1. IBSQL1.ParamByName('blob').AsBlob.LoadFromFile(MyFile);
error - External: SIGSEGV

What is the problem?
Looking the error it seem that ParamByName ('blob') does not exist?
How to do it correctly?
« Last Edit: June 30, 2021, 08:44:42 pm by sydenis »

devEric69

  • Hero Member
  • *****
  • Posts: 571
Re: IBX and BLOB update
« Reply #1 on: July 01, 2021, 09:48:05 am »
On one line you used FieldByName to save, but on the other line you wrote ParamByName to load: is this normal?
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

sydenis

  • New Member
  • *
  • Posts: 31
Re: IBX and BLOB update
« Reply #2 on: July 01, 2021, 02:20:59 pm »
I guess yes. Here is code sample:
read from db:
Code: Pascal  [Select][+][-]
  1.   with IBSLQ1 do
  2.   begin
  3.     SQL.Text:='select blob_field from tab1 where id=1;';
  4.     Transaction.StartTransaction;
  5.     ExecQuery;
  6.     FieldByName('blob_field').AsBlob.SaveToFile('image.bin');
  7.     .....
write to db:
Code: Pascal  [Select][+][-]
  1.   with IBSQL1 do
  2.   begin
  3.     SQL.Text:='update tab1 set blob_field=:blob_field where id=1;';
  4.     Transaction.StartTransaction;
  5.     ParamByName('blob_field').AsBlob.LoadFromFile('image.bin');
  6.     ExecQuery;  
  7.    .....
What is wrong?

tonyw

  • Full Member
  • ***
  • Posts: 228
    • MWA Software
Re: IBX and BLOB update
« Reply #3 on: July 03, 2021, 11:28:53 am »
Ubuntu 20.04 + Lazarus 2.0.12
I use the IBSQL component from ibx4lazarus2. 4-0 to access the Firebird 3 database
I need to write and read a BLOB field from my database.

There are no problems with reading from db:
Code: Pascal  [Select][+][-]
  1. IBSQL1.FieldByName('blob').AsBlob.SaveToFile(MyFile);
I get the desired file.

But writing to db does not work:
Code: Pascal  [Select][+][-]
  1. IBSQL1.ParamByName('blob').AsBlob.LoadFromFile(MyFile);
The problem is that you are using TIBSQL in the way that you would use TIBDataset. However, TIBSQL is working directly with the underlying Firebird interface and that requires a different approach.

The call to ParamByName('blob').AsBlob returns the interface IBlob. The input parameter has not yet been set and so this always returns nil - hence the exception.

To set the value of the blob, you have to create a TIBBlobstream, load it from a file and assign the IBlob interface returned by TIBBlobStream to the param e.g.

var b:TIBBlobStream;
begin
  b := TIBBlobStream.create;
 try
   b.Database := IBSQL.Database;
  b.Mode := bmWrite;
    b.LoadFromFile(...);
    IBSQL.ParamByName('blob').asBlob := b.blob;
  finally
    b.free;
  end;
« Last Edit: July 03, 2021, 11:32:58 am by tonyw »

sydenis

  • New Member
  • *
  • Posts: 31
Re: IBX and BLOB update
« Reply #4 on: July 05, 2021, 03:03:41 pm »
Tony, thank you very much! I used your advice and everything works fine.
My self I found a solution through asQuad:

Code: Pascal  [Select][+][-]
  1.    b.LoadFromFile(...);
  2.    b.Finalize;
  3.    IBSQL1.ParamByName('blob').AsQuad  := b.BlobID;

thank you again
« Last Edit: July 05, 2021, 03:41:46 pm by sydenis »

tonyw

  • Full Member
  • ***
  • Posts: 228
    • MWA Software
Re: IBX and BLOB update
« Reply #5 on: July 05, 2021, 03:49:52 pm »
Tony, thank you very much! I used your advice and everything works fine.
My self I found a solution through asQuad:

Code: Pascal  [Select][+][-]
  1.    b.LoadFromFile(...);
  2.    b.Finalize;
  3.    IBSQL1.ParamByName('blob').AsQuad  := b.BlobID;

thank you again
Yes - that is an equivalent statement.

 

TinyPortal © 2005-2018