如何在 SQLite 中分段更新 blob? (How to update piecewise a blob in SQLite?)


問題描述

如何在 SQLite 中分段更新 blob? (How to update piecewise a blob in SQLite?)

I have large blobs in an SQLite app, and need to update small segments of these blobs in a piecewise fashion. Something like saying "update bytes X through Y of blob B with data D", this can be done in other databases with Blob manipulating functions, but I can't find anything like this for SQLite, am I stuck? Or does SQLite have ways of manipulating Blobs?

Thanks.


參考解法

方法 1:

This is not directly an answer to your question, but I have some experience using random access for (big) blobs in SQLite, and I advise you against using it, if you can. Here's why:

Blobs break the SQL query format entirely. If your blob data needs any kind of processing, it will certainly at some point need filtering. Whatever mechanism you have in place to deal with filtering in SQL will be useless in this regard.

Dealing with binary blobs wrapped in databases opposed to binary data in raw files limits your options. You will not be able to randomly read and write to data at the same time from multiple processes, which is possible with files. You can't use any tool that deals with this data and provides only a file I/O interface. You can't truncate or resize the blob. Files are simply a lot more versatile.

It may seem convenient to have everything contained within one file, as it simplifies backup and transfer, but the pain of working with blobs is simply not worth it.

So as your attorney, I advise you to write your blobs as raw files to the file system, and merely store a reference to the filename in your database. If your blobs are rather small and guaranteed not to grow, forget my advice.

方法 2:

SQLite 3.x supports this through the sqlite3_blob_write function.

int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset)

Note that this function is provided as part of the SQLite 3 C/C++ API. You'll need to program against this directly to use it.

If you're using some other higher level wrapper such as the System.Data.SQLite, last time I looked, you will not have access to this function.  

方法 3:

I strongly agree with everything said by paniq. Using BLOBs limits your options considerably.

If you are using System.Data.SQLite you won't have real support for BLOBs. That's why I wrote my own class to handle them. You can find the code here: Sample BLOB code

Note that SQLite has several potential pitfalls for those who dare to work with BLOBs. One of the major problems is that SQLite loads the entire BLOB field to memory before allowing you to even check their length.. So expect a lot of memory thrashing if you have large BLOB fields... 

(by Robert GouldpaniqAshLiron Levi)

參考文件

  1. How to update piecewise a blob in SQLite? (CC BY-SA 3.0/4.0)

#blob #sqlite






相關問題

Hibernate:如何設置自定義 UserType 的值 (Hibernate: How to set the value of a custom UserType)

從數據庫中讀取 zip 存檔 (Reading zip archive from database)

從 JSON 網絡服務檢索 android 中的 byte[] (Retrieve byte[] in android from JSON webservice)

ORA-01465: 使用 BLOB 時,oracle 中的十六進制數無效 (ORA-01465: invalid hex number in oracle while using BLOB)

無法使用 fopen/fwrite/fclose (php) 在 mysql 中保存 xml 文件內容 (Can't save xml file content in mysql with fopen/fwrite/fclose (php))

將 Azure Blob 中的數據流式傳輸回客戶端 (Streaming Data from Azure Blobs Back to Client)

我希望將畫布保存為 mySql blob 字段中的 blob (I am looking to save a canvas as a blob in mySql blob field)

如何在 SQLite 中分段更新 blob? (How to update piecewise a blob in SQLite?)

YugabyteDB 中是否像 Oracle 或 Postgres 那樣支持 BLOB? (Is there support for BLOBs in YugabyteDB like in Oracle or Postgres?)

使用顫振將圖像作為blob存儲在mysql數據庫中 (Storing image's as blob in mysql database with flutter)

如何從 docxtemplater.js 輸出 docx 以在 jszip 中輸入 (How can I output a docx from docxtemplater.js for input in jszip)

不同語言的 Blob 請求 (Blob request in different language)







留言討論