Recent

Author Topic: BLOB cast as varchar shows only 255 characters (SQLite3)  (Read 4429 times)

WJSwanepoel

  • New Member
  • *
  • Posts: 25
BLOB cast as varchar shows only 255 characters (SQLite3)
« on: August 09, 2017, 09:50:32 am »
I am new to Lazarus.

When selecting a field from SQLite3 database (stored as a BLOB) I use a select:  "Select cast(data as varchar) from content;"

However when I display this field in a TDBGrid or TDBMemo it shows only the first 255 characters.

What am I doing wrong?

balazsszekely

  • Guest
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #1 on: August 09, 2017, 10:18:01 am »
@WJSwanepoel

I hope you're not a spammer. If not welcome to the forum.
1. Remove the cast from the SQL, use it like this:  "select DATA from CONTENT;"
2. Double click your query, a dialog should popup with the available fields, select "DATA" then go to ObjectInspector-->Events-->OnGetText
3: I assume the blob is text type not binary
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SQLQuery1DATAGetText(Sender: TField; var aText: string;
  2.   DisplayText: Boolean);
  3. var
  4.  Ms: TStream;
  5. begin
  6.   DisplayText := False;
  7.   if not TField(Sender).IsNull then
  8.   begin
  9.     Ms := SQLQuery1.CreateBlobStream(TField(Sender), bmRead);
  10.     try
  11.       if Ms.Size > 0 then
  12.       begin
  13.         Ms.Position := 0;
  14.         SetLength(aText, Ms.Size);
  15.         Ms.Read(Pointer(aText)^, Ms.Size);
  16.         DisplayText := True;
  17.       end;
  18.     finally
  19.       Ms.Free;
  20.     end;
  21.   end;
  22. end;
  23.  

Please note: the grid cannot display multiple lines, at least not properly. If your blob contains multiple line you should should show in a separate memo, or even better a TDBMemo.

WJSwanepoel

  • New Member
  • *
  • Posts: 25
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #2 on: August 09, 2017, 10:22:14 am »
Thanks you - I am for sure not a spammer. I am a retired Data warehousing specialist (using Teradata). I try to keep my brain going by doing some programming just for fun.

balazsszekely

  • Guest
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #3 on: August 09, 2017, 10:26:29 am »
@WJSwanepoel
Quote
I am for sure not a spammer. I am a retired Data warehousing specialist (using Teradata). I try to keep my brain going by doing some programming just for fun.
We had a lot of spammer lately, but I'm glad you're not one of them. Welcome to the forum :).

WJSwanepoel

  • New Member
  • *
  • Posts: 25
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #4 on: August 09, 2017, 10:29:19 am »
Double-clicking the query shows no fields?? May I set up small example and send it to you?

balazsszekely

  • Guest
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #5 on: August 09, 2017, 10:34:14 am »
Quote
Double-clicking the query shows no fields?
1. Connect to the database(set MySQlXXConnection.Connected property to true)
2. Set SQLQuery1.Active property to true
3. Double-click the query
4. Then click the + button to add fields

Quote
May I set up small example and send it to you?
Yes. If the above method is not working, please upload the project somewhere and send me the link.
« Last Edit: August 09, 2017, 10:36:52 am by GetMem »

WJSwanepoel

  • New Member
  • *
  • Posts: 25

balazsszekely

  • Guest
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #7 on: August 09, 2017, 11:25:31 am »
@WJSwanepoel
1. Download and install RichMemo(http://wiki.freepascal.org/RichMemo#Download  or from the next post attachment)
2. See attached project(don't forget to copy sqlite3.dll to the project/system folder

« Last Edit: August 09, 2017, 11:32:54 am by GetMem »

balazsszekely

  • Guest
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #8 on: August 09, 2017, 11:27:10 am »
I attached richmemo to this post, so you can download more easily.

WJSwanepoel

  • New Member
  • *
  • Posts: 25
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #9 on: August 09, 2017, 11:43:02 am »
Thank very much - will try it.

balazsszekely

  • Guest
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #10 on: August 09, 2017, 12:06:00 pm »
Quote
Thank very much - will try it.
You're welcome. Try the attached demo, I modified a few things in the meantime.

WJSwanepoel

  • New Member
  • *
  • Posts: 25
Re: BLOB cast as varchar shows only 255 characters (SQLite3)
« Reply #11 on: August 09, 2017, 12:07:43 pm »
Works like a charm - THANK YOU!!!

 

TinyPortal © 2005-2018