Delphi-如何在SQLite数据库中将位图保存到BLOB字段 [英] Delphi - How to save bitmap to BLOB field in SQLite database
问题描述
我试图将TBitmap图像数组添加到ClientDataSet的不同记录中(在ftBlob字段中),然后将这些记录保存到SQLite数据库中. BLOB字段(DocImage)是数据库中的必填字段.
I am trying to add an array of TBitmap images to different records of a ClientDataSet (in a ftBlob field) and then save those records to a SQLite database. The BLOB field (DocImage) is a required field in the database.
但是,我的代码似乎根本没有将位图保存到ClientDataSet中的blob字段中.因此,当我调用BdmMain.cdsTrxDoc.ApplyUpdates(0)
时,出现错误:字段'DocImage'必须具有一个值."
However, my code does not seem to save the bitmaps to the blob field in the ClientDataSet at all... So when I call BdmMain.cdsTrxDoc.ApplyUpdates(0)
, I get the error: "Field 'DocImage' must have a value."
在调用FTrxPhotoValue[i].SaveToStream(BlobStream)
之前和之后,我已经检查了BlobStream(BlobStream.Size)的大小,并且确实增加了大小,但是BlobField的数据大小仍然为0.
I have checked the size of my BlobStream (BlobStream.Size) before and after calling FTrxPhotoValue[i].SaveToStream(BlobStream)
and it does increase in size, but BlobField's data size remains 0.
以下是代码段:
FTrxIDValue: Integer;
FTrxDocIDValue: array of Integer;
FTrxPhotoValue: array of TBitmap;
// ...
for i := 0 to Length(FTrxPhotoValue) do
begin
BdmMain.cdsTrxDoc.Insert;
BdmMain.cdsTrxDoc['TrxID'] := FTrxIDValue;
BdmMain.cdsTrxDoc['DocID'] := FTrxDocIDValue[i];
BlobField := BdmMain.cdsTrxDoc.FieldByName('DocImage');
BlobStream := BdmMain.cdsTrxDoc.CreateBlobStream(BlobField, bmWrite);
FTrxPhotoValue[i].SaveToStream(BlobStream);
end;
// ...
BdmMain.cdsTrxDoc.ApplyUpdates(0);
推荐答案
调用SaveToStream
后,应Free
该流.该流仅在Destroy
期间更新基础字段.
You should Free
the stream after the call to SaveToStream
. The stream will update the underlying field only during Destroy
.
这篇关于Delphi-如何在SQLite数据库中将位图保存到BLOB字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!