我试图将图像从SQLite数据库中的Blob加载到ListView中。
这是我的SQLite数据库:
CREATE TABLE [Pictures](
[PicId] INT,
[UsersImage] BLOB);
代码如下:
procedure LoadFromBlob;
var
BlobStream: TStream;
begin
Form4.FDConnection1.Connected := True;
try
Form4.FDQuery1.Active := True;
Form4.viewimgquery.Active := True;
Form4.FDQuery1.Open;
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);
// access a string from a field like this
if (Form4.viewimgquery.FieldByName('PicId')
.AsInteger = Form4.FDQuery1.FieldByName('ID').AsInteger) then
begin
// load your blob stream data into a control
Form4.viewimage.Bitmap.LoadFromStream(BlobStream);
Form4.ListView1.items.Add();
BlobStream.Free;
Form4.viewimgquery.Next;
end;
end;
except
on e: Exception do
begin
// ShowMessage(e.Message);
end;
end;
Form4.FDConnection1.Connected := False;
end;
我与viewimage查询一起使用的SQL很简单:
SELECT *
FROM PICTURES
WHERE PICID = :PicId
如果我将SQL更改为(select * from pictures
)并将查询设置为在设计时处于活动状态,则会看到所有图像都已加载到ListView中。所以这告诉我我的实时绑定设置正确。
我的问题是,当我调用该过程时,没有任何东西加载到ListView中。
我不确定我的问题是否出在SQL状态或代码上。