我有一个Android应用程序试图从SQLite数据库加载blob作为字节数组:
public byte[] getBytesForId(long id){
byte[] result = null;
String sql = "SELECT _blob FROM test_table WHERE _id=?";
String[] selection = new String[] { String.valueOf(id) };
Cursor mCursor = super.DB().rawQuery(sql, selection);
if ( mCursor != null && mCursor.moveToFirst() ) {
if (mCursor.getCount() > 0)
result = mCursor.getBlob(0);
if (mCursor != null && !mCursor.isClosed())
mCursor.close();
}
return result;
}
但我得到以下例外:
Failed to read row 0, column 0 from a CursorWindow which has 0 rows, 1 columns.
java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetBlob(Native Method)
at android.database.CursorWindow.getBlob(CursorWindow.java:404)
at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
at com.test.testapp.model.DbClass.getBytesForId(DbClass.java:218)
at com.test.testapp.TestService.StepThroughBlocks(TestService.java:434)
at com.test.testapp.TestService.checkBlocks(TestService.java:378)
at com.test.testapp.TestService.access$4(TestService.java:357)
at com.test.testapp.TestService$2.run(TestService.java:315)
at java.lang.Thread.run(Thread.java:841)
当然,如果我先检查光标是否有某些数据......
if ( mCursor != null && mCursor.moveToFirst() )
if (mCursor.getCount() > 0)
result = mCursor.getBlob(0);
......我不应该得到这个例外吗?我添加了mCursor.getCount行,希望这可能有所帮助,但我仍然得到异常。这怎么可能?