从 Android sqlite 数据库中检索大 blob [英] Retrieve large blob from Android sqlite database

查看:41
本文介绍了从 Android sqlite 数据库中检索大 blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 应用程序的 sqlite 数据库中存储了大量二进制数据 (protobufs),却没有意识到 Android 的 Cursor 最多只能容纳 1MB 的数据.我现在知道我应该将这些二进制 blob 存储在文件中,并且只引用 sqlite 数据库条目中的文件.

I stored chunks of binary data (protobufs) in the sqlite database of an Android app without realizing that Android's Cursor can only hold a maximum of 1MB of data. I now know that I should have stored these binary blobs in files and only referenced the files in the sqlite database entries.

我需要升级数据库(该应用程序已经使用了一段时间)以便将这些二进制块移动到文件中.问题是某些用户的数据可能已经超过了 1MB 的限制,我无法从数据库中检索它(访问包含大 blob 的单行的结果 Cursor 会引发 Cursorcode>IllegalStateException: 无法从 CursorWindow 读取第 0 行、第 0 列.请确保在从 Cursor 访问数据之前已初始化 Cursor ).

I need to upgrade the database (the app has been in use for a while) in order to move these binary chunks to files. The problem is that some user's data may have already exceeded the 1MB limit and I'm not able to retrieve it from the database (accessing the resulting Cursor for a single row that contains a large blob throws an IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialize before accessing data from it).

如何检索已存储在 sqlite 数据库中的大于 1MB Cursor 限制的二进制 blob?

How do I retrieve the binary blobs that are greater than the 1MB Cursor limit that have been stored in the sqlite database?

推荐答案

您可以分段读取大块.首先找出哪些需要这种处理:

You can read large blobs in pieces. First find out which ones need this treatment:

SELECT id, length(blobcolumn) FROM mytable WHERE length(blobcolumn) > 1000000

然后使用substr读取块:

SELECT substr(blobcolumn,       1, 1000000) FROM mytable WHERE id = 123
SELECT substr(blobcolumn, 1000001, 1000000) FROM mytable WHERE id = 123
...

<小时>

您也可以编译自己的 SQLite 副本并访问 BLOB 流 I/O 函数或带有 NDK 的 C API 的普通查询函数,但在这种情况下会太复杂.


You could also compile your own copy of SQLite and access either the BLOB stream I/O functions or the normal query functions of the C API with the NDK, but that would be too complex in this case.

这篇关于从 Android sqlite 数据库中检索大 blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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