有什么能及的QUOT事业; java.lang.IllegalStateException:从排山坳0 0场插槽失败"插槽、有什么、山坳、事业

2023-09-07 12:16:23 作者:沉浸在离心的边缘 -

我有抛出这个异常的源码数据库上运行的SQL查询:java.lang.IllegalStateException:拿到场插槽从排山坳0 0失败当我打电话:

I have an SQL query running on an 'sqlite' database that is throwing this exception: "java.lang.IllegalStateException: get field slot from row 0 col 0 failed" when I call:

db.rawQuery( "SELECT data FROM session_data WHERE session_id = ?", new String[] { String.format("%d", session_id) } );
if (!cursor.moveToFirst()) return;
bytes[] bytes = cursor.getBlob(0);

列存在,如果我登录使用亚行外壳sqlite3的/path/to/database.db 我可以成功地执行相同的查询。

The column exists, and if I log in using adb shell sqlite3 /path/to/database.db I can successfully execute the same query.

而产生光标显示,与名为数据一栏选择1排。 BLOB的大小约为1.5 MB - 会是呢?我已验证 cursor.GetColumnIndex(数据)返回0,所以列存在。

The resulting cursor shows that there is 1 row selected with 1 column whose name is "data". The size of the blob is about 1.5 MB - could that be it? I have verified that cursor.GetColumnIndex("data") returns 0, so the column exists.

这是在Android 2.1 SDK运行。任何想法?

This is running on Android 2.1 SDK. Any ideas?

推荐答案

问题是,BLOB太大。看来, cursor.getBlob(0)如果BLOB大小超过一兆字节失败。阅读部分的BLOB解决我的问题:

The problem was that the blob was too big. It appears that cursor.getBlob(0) fails if the blob size is over a megabyte. Reading the blob in segments solved my issue:

// get length of blob data
Cursor cursor = db.rawQuery( "SELECT LENGTH(data) AS len FROM table WHERE id = ?", etc);
if (cursor == null || !cursor.moveToFirst()) throw new Exception("failed");
int size = cursor.getInt(cursor.getColumnIndexOrThrow("len"));
cursor.close();

// read a segment of the blob data
cursor = db.rawQuery( "SELECT SUBSTR(data,0,500000) AS partial FROM table WHERE id = ?", etc);
if (cursor == null || !cursor.moveToFirst()) throw new Exception("failed");
byte[] partial = cursor.getBlob(cursor.getColumnIndexOrThrow("partial"));
cursor.close();

// copy partial bytes, repeat until the whole length has been read...