Read / write blob field lazarus / 16-Databases ---------------------------------------------------------------------- LemonParty · 2025-09-05 15:13 Hello. How to read / write a blob field to my variable? ---------------------------------------------------------------------- cdbc · 2025-09-05 16:28 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'  Regards Benny ---------------------------------------------------------------------- LemonParty · 2025-09-05 17:51 I use SQLite and have a field declared as Blob. It is not string. ---------------------------------------------------------------------- cdbc · 2025-09-05 18:05 Hi What's in it?!? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnLoadClick(Sender: TObject);var lstrm: TStream;begin  ...  Query1.Open;  ...  lstrm:= Query1.CreateBlobStream(Query.FieldByName('photo'),bmRead);  lstrm.Position:= 0;  Picture1.Graphic.LoadFromStream(lstrm);  lstrm.Free;  ...end;Regards Benny ---------------------------------------------------------------------- LemonParty · 2025-09-05 20:10 Isn't this going to work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ReadVar(const aVar; aSize: SizeUInt): TBytes;inline;begin  SetLength(Result, aSize);  Move(aVar, Result[0], aSize);end; procedure WriteVar(const B: TBytes; var aVar);inline;begin  Move(B[0], aVar, Length(B));end; Than used this way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   WriteVar(Query.FieldByName('MyBlobField').AsBytes, MyVar);  {...}  Query.FieldByName('MyBlobField').AsBytes:= ReadVar(MyVar, SizeOf(MyVar)); ---------------------------------------------------------------------- cdbc · 2025-09-05 22:02 Hi I think these are better:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program bytestovar; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes, sysutils  { you can add units after this }; procedure BytesToVar(const B: TBytes; var aVar; var aSize: sizeint); inline;begin { old win32 api trick }  if aSize < 0 then begin aSize:= Length(B); exit; end;  Move(B[0],pointer(aVar)^, Length(B));end; function VarToBytes(const aVar; aSize: SizeUInt): TBytes; inline;begin  SetLength(Result, aSize);  Move(pointer(aVar)^, Result[0], aSize);end; var  aVar,ls: string;  blob: TBytes;  b: byte;  sz: sizeint = -1; begin  if ParamCount >= 1 then ls:= ParamStr(1)  else begin writeln('(!) Try again with "some text" in first param.'); halt(1); end;  writeln('(i) Input.: ',ls);  blob:= VarToBytes(ls,length(ls));  write('(i) TBytes: ');  for b in blob do write(b.ToHexString(2),' ');  writeln;  { if we call BytesToVar with a negative 'aSize' it will return the size needed in 'aSize' }  BytesToVar(blob,aVar,sz); { sz was initialized to -1, but returns with the actual size }  aVar:= StringOfChar(#0,sz); // got the size, we have to allocate the memory beforehand  { the thing is... in BytesToVar we have no chance of finding out the type of 'aVar' }  BytesToVar(blob,aVar,sz);  writeln('(i) aVar..: ',aVar);end.(* The above "Shenanigans" are old tricks from win32 API *)  Regards Benny