How to load an image(Blob) from a sQlite into a timage, and a listview.

Grant1842
Grant1842 used Ask the Experts™
on
This is an Delphi MultiDevice app. Using Firedac Query and sQlite database
I am trying to load an image from a blob in an sqlite database into a ListView.
The database DDL
CREATE TABLE [Pictures](
    [PicId] INT, 
    [UsersImage] BLOB);

Open in new window


Here is my code
procedure LoadFromBlob;
var
  BlobStream: TStream;
begin
  // FireDAC.Connected := True;
  try
    AccessCameraAppForm.FDQuerySelect.Open;
    AccessCameraAppForm.FDQuerySelect.First;
    while (not AccessCameraAppForm.viewimgquery.EOF) do
    begin
      // access a stream from a blob like this
      BlobStream := AccessCameraAppForm.viewimgquery.CreateBlobStream
        (AccessCameraAppForm.viewimgquery.FieldByName('UsersImage'),
        TBlobStreamMode.bmRead);
      // access a string from a field like this
      if (AccessCameraAppForm.viewimgquery.FieldByName('PicId')
        .AsInteger = AccessCameraAppForm.FDQuery1.FieldByName('ID').AsInteger)
      then
      begin
        // load your blob stream data into a control
        AccessCameraAppForm.viewimage.Bitmap.LoadFromStream(BlobStream);
         AccessCameraAppForm.ListView2.items.Add();
        BlobStream.Free;
        AccessCameraAppForm.viewimgquery.Next;
      end;
      end;
    except
      on e: Exception do
      begin
        // ShowMessage(e.Message);
      end;
    end;
    // FireDAC.Connected := False;
  end;

Open in new window

I am useing the live bindings to bind the querys image to the listviews item.image2
The code compiles but can not load the image from the sqlite database into the listview or a timage.
Thanks for any help .
This seems to be a problem for me.
I have attached the sorcecode.
Project4.zip
Here is the database
flatrate.db3


I am useing differnt sql here.
Differnt-Sql-Project4.zip
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sinisa VukSoftware architect
Top Expert 2012

Commented:
Because you set parameter inside of viewimgquery (_PicId), then you need to fill it first.
I reuse value from your code - but code is quite confusing. But here you are...

form4.FDQuery1.Close;
    form4.FDQuery1.Open;
    Form4.viewimgquery.Close;
    // access a string from a field like this
    form4.viewimgquery.ParamByName('_PicId').AsInteger := form4.FDQuery1.FieldByName('ID').AsInteger;
    Form4.viewimgquery.Open;
    Form4.viewimgquery.First;
    while (not form4.viewimgquery.EOF) do
    begin
      // access a stream from a blob like this
      BlobStream := form4.viewimgquery.CreateBlobStream(
        form4.viewimgquery.FieldByName('UsersImage'),
        TBlobStreamMode.bmRead);
      try
        form4.viewimage.Bitmap.LoadFromStream(BlobStream);
        itm := form4.ListView1.items.Add();
      finally
        BlobStream.Free;
      end;

      form4.viewimgquery.Next;
    end;

Open in new window


..just to see something....
THanks for your help.  
What does  Itm equal
itm := form4.ListView1.items.Add();
Sinisa VukSoftware architect
Top Expert 2012

Commented:
This is for accessing newly created ListViewItem. With them you can:

- change label:
itm.Text := ...

- set image index:
itm.ImageIndex := ...

and so on...
Geert GOracle dba
Top Expert 2009

Commented:
why are you mixing procedural and object oriented code ?
that's just making your life difficult

if you create a second instance of TForm4 named formXYZ like:
formXYZ := TForm4.create(application);

Open in new window


that procedure won't work.
but ... correctly :
  private
    { Private declarations }
     procedure LoadFromBlob;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.fmx}
 procedure TForm4.LoadFromBlob;

Open in new window


and then you don't need to use Form4, which possibly doesn't exist:
sample:
      if viewimgquery.FieldByName('PicId').AsInteger = FDQuery1.FieldByName('ID').AsInteger then 

Open in new window

Geert GOracle dba
Top Expert 2009

Commented:
that was just to say something about the wrong code
and yes ... confusing, sinisa got that right :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial