Delphi - How to save bitmap to BLOB field in SQLite database

亡梦爱人 提交于 2020-01-24 13:24:10

问题


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.

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."

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.

Here is a snippet of the code:

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);

回答1:


You should Free the stream after the call to SaveToStream. The stream will update the underlying field only during Destroy.



来源:https://stackoverflow.com/questions/46099167/delphi-how-to-save-bitmap-to-blob-field-in-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!