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

查看:513
本文介绍了从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的限制,我不能从数据库检索它(访问结果 Cursor 一个大的blob抛出一个 IllegalStateException:无法从CursorWindow读取第0行,第0列。确保游标在初始化之前访问它的数据)。

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

and then read chunks with 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 功能或访问C API与NDK,但在这种情况下会太复杂。


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天全站免登陆