Small problem - putting strings into BLOB Type 1 field

Discussion of open issues, suggestions and bugs regarding IBDAC (InterBase Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
zd
Posts: 78
Joined: Sun 01 Jul 2007 13:16

Small problem - putting strings into BLOB Type 1 field

Post by zd » Sun 01 Jul 2007 13:34

Hi!

I'm migrating from MySQL to FireBird 2.0 and while porting my code, I bumped into this problem.

The equivalent of MySQL "Text" type field is "Blob TYPE 1" in Firebird.

The problem is that when trying to parameterize a query, IBDAC 2.0 doesn't accept Strings the way it should do.

Like:

DBQuery.SQL.Text:='INSERT INTO TABLE(MyText) VALUES(:txt)';
DBQuery.ParamByName('txt').AsString:=Edit1.Text;
DBQuery.ExecSQL;

MyText is BLOB Type 1 of course.

The above yields the error "can't move data into blob field" or something to that extent.

If I modify the second line by changing AsString into AsBlob
DBQuery.ParamByName('txt').AsBlob:=Edit1.Text;
The problem is solved.

Why I'm asking for help is because I'm already modifying a huge base of code, and it would annoying if I had to rewrite all my "AsString" params to "AsBlob"...

Is there are workaround for this without me having to modify a lot of code?

Thanks in Advance!

Z

Alex
Posts: 655
Joined: Mon 08 Nov 2004 08:39

Post by Alex » Mon 02 Jul 2007 08:29

Unlike MySQL, InterBase and Firebird BLOB fields are quite different from VARCHAR fields. These servers do not allow to insert VARCHAR data to BLOB fields. When you set the IBCQuery.SQL property with insert SQL, IBDAC doesn't know parameters datatypes. When you set parameter value (ParamByName('txt').AsString) you implicitly specify parameter datatype, so this is the reason of the problem. To resolve this issue, you should explicitly or implicitly specify parameter datatype:
DBQuery.SQL.Text:='INSERT INTO TABLE(MyText) VALUES(:txt)';
DBQuery.ParamByName('txt').Datatype := ftBlob;
DBQuery.ParamByName('txt').AsString:=Edit1.Text;
DBQuery.ExecSQL;
or
DBQuery.SQL.Text:='INSERT INTO TABLE(MyText) VALUES(:txt)';
DBQuery.ParamByName('txt').AsBlob:=Edit1.Text;
DBQuery.ExecSQL;

zd
Posts: 78
Joined: Sun 01 Jul 2007 13:16

Post by zd » Mon 02 Jul 2007 14:14

Thank you Alex, now I understand :-)

Post Reply