Recent

Author Topic: Read / write blob field  (Read 481 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 373
Read / write blob field
« on: September 05, 2025, 05:13:01 pm »
Hello.

How to read / write a blob field to my variable?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2473
    • http://www.cdbc.dk
Re: Read / write blob field
« Reply #1 on: September 05, 2025, 06:28:31 pm »
Hi
What kind of blob?!?
If it is text, then you can use the new features in my 'IStringList', which you can find HERE.
...and attached is a demo-app for sqlite3, which amo. deals with memo-blobs... And it shows how to use the new features of 'IStringList'  :D
Regards Benny
« Last Edit: September 05, 2025, 07:10:29 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

LemonParty

  • Sr. Member
  • ****
  • Posts: 373
Re: Read / write blob field
« Reply #2 on: September 05, 2025, 07:51:16 pm »
I use SQLite and have a field declared as Blob. It is not string.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2473
    • http://www.cdbc.dk
Re: Read / write blob field
« Reply #3 on: September 05, 2025, 08:05:43 pm »
Hi
What's in it?!?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnLoadClick(Sender: TObject);
  2. var lstrm: TStream;
  3. begin
  4.   ...
  5.   Query1.Open;
  6.   ...
  7.   lstrm:= Query1.CreateBlobStream(Query.FieldByName('photo'),bmRead);
  8.   lstrm.Position:= 0;
  9.   Picture1.Graphic.LoadFromStream(lstrm);
  10.   lstrm.Free;
  11.   ...
  12. end;
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

LemonParty

  • Sr. Member
  • ****
  • Posts: 373
Re: Read / write blob field
« Reply #4 on: September 05, 2025, 10:10:10 pm »
Isn't this going to work:
Code: Pascal  [Select][+][-]
  1. function ReadVar(const aVar; aSize: SizeUInt): TBytes;inline;
  2. begin
  3.   SetLength(Result, aSize);
  4.   Move(aVar, Result[0], aSize);
  5. end;
  6.  
  7. procedure WriteVar(const B: TBytes; var aVar);inline;
  8. begin
  9.   Move(B[0], aVar, Length(B));
  10. end;

Than used this way:
Code: Pascal  [Select][+][-]
  1.   WriteVar(Query.FieldByName('MyBlobField').AsBytes, MyVar);
  2.   {...}
  3.   Query.FieldByName('MyBlobField').AsBytes:= ReadVar(MyVar, SizeOf(MyVar));
  4.  
« Last Edit: September 05, 2025, 10:14:28 pm by LemonParty »
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2473
    • http://www.cdbc.dk
Re: Read / write blob field
« Reply #5 on: September 06, 2025, 12:02:09 am »
Hi
I think these are better:
Code: Pascal  [Select][+][-]
  1. program bytestovar;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes, sysutils
  10.   { you can add units after this };
  11.  
  12. procedure BytesToVar(const B: TBytes; var aVar; var aSize: sizeint); inline;
  13. begin { old win32 api trick }
  14.   if aSize < 0 then begin aSize:= Length(B); exit; end;
  15.   Move(B[0],pointer(aVar)^, Length(B));
  16. end;
  17.  
  18. function VarToBytes(const aVar; aSize: SizeUInt): TBytes; inline;
  19. begin
  20.   SetLength(Result, aSize);
  21.   Move(pointer(aVar)^, Result[0], aSize);
  22. end;
  23.  
  24. var
  25.   aVar,ls: string;
  26.   blob: TBytes;
  27.   b: byte;
  28.   sz: sizeint = -1;
  29.  
  30. begin
  31.   if ParamCount >= 1 then ls:= ParamStr(1)
  32.   else begin writeln('(!) Try again with "some text" in first param.'); halt(1); end;
  33.   writeln('(i) Input.: ',ls);
  34.   blob:= VarToBytes(ls,length(ls));
  35.   write('(i) TBytes: ');
  36.   for b in blob do write(b.ToHexString(2),' ');
  37.   writeln;
  38.   { if we call BytesToVar with a negative 'aSize' it will return the size needed in 'aSize' }
  39.   BytesToVar(blob,aVar,sz); { sz was initialized to -1, but returns with the actual size }
  40.   aVar:= StringOfChar(#0,sz); // got the size, we have to allocate the memory beforehand
  41.   { the thing is... in BytesToVar we have no chance of finding out the type of 'aVar' }
  42.   BytesToVar(blob,aVar,sz);
  43.   writeln('(i) aVar..: ',aVar);
  44. end.
  45. (* The above "Shenanigans" are old tricks from win32 API *)  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018