2017-06-28 3 views
-1

私のinv.dbには以下の内容があります修正方法アンドロイドのカーソル初期化エラー?

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を除く4つの列があります。

// 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つの列がありますが、アンドロイドはID、名前、価格の3つのみを表示しています。しかし、ターミナルでコマンドを実行すると、私はすべてを見ることができます。列名はすべて正しいです。 Ivは手作業で値3と4を添えてエラーを生成しました – Xera12

+0

あなたはcursor.getColumnNamesを試しましたか? –

+0

_id、名前、および価格を列名としてログに出力します。しかし、表スキーマはCREATE TABLEインベントリーです(_ID INTEGER PRIMARY KEY AUTOINCREMENT、TEXT NOT NULL、サプライヤーTEXT、数量INTEGER NOT NULL DEFAULT 0、価格INTEGER NOT NULL DEFAULT 0)。 – Xera12

関連する問題