2017-06-28 52 views
-1

我的inv.db包含以下內容如何解決android中的遊標初始化錯誤?

select * from inventory;

id|name|supplier|quanity|price 

1|Product 1|Wallmart|10|50 
2|Product 1|Wallmart|10|50 

但是我得到的錯誤

Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 3 columns. 
Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 

但也有四列不包括ID。

// Find the columns of pet attributes that we're interested in 
     int nameColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_NAME); 
     int priceColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_PRICE); 
     int quanColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_QUAN); 
     int supColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_SUP); 


     // Read the pet attributes from the Cursor for the current pet 
     String petName = cursor.getString(nameColumnIndex); 
     String price = cursor.getString(priceColumnIndex); 
     String quantity = cursor.getString(quanColumnIndex); // produces error 

林不知道這裏有什麼問題。我的代碼運行,如果我刪除的代碼

回答

0

你的光標正確初始化最後一行,但它不能在InventoryContract.InventoryEntry.COLUMN_PRO_QUAN

找到字符串列索引確保存儲字符串有匹配的列名是什麼。現在它可能是不正確

+0

其實有5列 - 但Android只看到3即id,名稱和價格。但是當我在終端上運行命令我可以看到所有。列名是完全正確的。 Iv嘗試手動將值3和4的索引產生錯誤 – Xera12

+0

你試過cursor.getColumnNames? –

+0

它將_id,名稱和價格打印爲列名稱。但是表模式是CREATE TABLE inventory(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,supplier TEXT,quantity INTEGER NOT NULL DEFAULT 0,price INTEGER NOT NULL DEFAULT 0); – Xera12

相關問題