2011-05-23 45 views
0

我有一個SQL查詢運行在引發此異常的'sqlite'數據庫:「java.lang.IllegalStateException :從行字段插槽0山坳0失敗」當我打電話:什麼可能是「java.lang.IllegalStateException異常:從第0行col 0失敗的字段插槽」的原因

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); 

列存在,如果我登錄使用adb shell sqlite3 /path/to/database.db我可以成功地執行相同的查詢。

生成的光標顯示有1行被選中,其中1列的名稱是「data」。 blob的大小約爲1.5 MB - 可以嗎?我已驗證cursor.GetColumnIndex("data")返回0,所以列存在。

這是在Android 2.1 SDK上運行的。有任何想法嗎?

回答

3

的問題是,斑太大。看起來cursor.getBlob(0)失敗,如果blob大小超過兆字節。讀段的blob解決了我的問題:

// 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... 
1

我假設您持有從rawQuery光標的引用。 在從光標獲得任何值之前,您需要撥打cursor.moveToFirst(),因爲cursor最初位於-1。正確的方法是:

Cursor cursor = db.rawQuery("SELECT data FROM session_data WHERE session_id = ?", new String[] { String.format("%d", session_id) }); 
if(cursor!= null && cursor.moveToFirst()){ // checking if the cursor has any rows 
    bytes[] bytes = cursor.getBlob(0); 
} 

,並確保您的數據庫列blob

+0

我實際上是這樣做的 - 我已經更新了這個問題。謝謝。 – 2011-05-23 21:29:02

相關問題