Delphi-如何在SQLite数据库中将位图保存到BLOB字段 [英] Delphi - How to save bitmap to BLOB field in SQLite database

查看:493
本文介绍了Delphi-如何在SQLite数据库中将位图保存到BLOB字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆