CursorIndexOutOfBoundSexceptionを取得します
-
12-12-2019 - |
質問
DB.BUTからデータを読み取る機能を持っていますこれは常に誤りを返します.Androidで多くの知識を持っていません。
10-03 11:33:05.870: E/AndroidRuntime(1321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.myworldsafe/com.projects.myworldsafe.DeailedView}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
.
。
String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+indexd);
return c.getString(index);
}
. 解決
これを使用:
return c.getString(indexd);
.
あなたが実際に価値を取得したいものです。index
を使用しました。これはカーソル内のレコード数を返します。
またもっと良い練習は次のとおりです。
String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);
if(c.getCount()>0){
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+index);// use index instead of indexd to print total records in query
return c.getString(index);// return value of column number specified in indexd
}
else
return "";// when no records found
}
. 他のヒント
これは正しいコード
ですint index= c.getCount(); // this will return total rows found
int indexd= c.getColumnIndex(ITEM_CONTENT); // this is the column index by giving the column name.
System.out.print("Count query"+indexd);
return c.getString(indexd); // here you need to pass the column index but you passing the index which was total number of rows
. Cursor c=db.rawQuery(selectQuery,params);
.
サイズ0でカーソルを返し、コードを変更してカーソルに項目があるかどうかを確認してから、次の位置に移動します。
Cursor c=db.rawQuery(selectQuery,params);
if(c.getCount()>0)
{
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+indexd);
return c.getString(index);
}
return "";
. あなたは空の結果セットから読み込もうとしているように聞こえます。
データベースを確認すると、クエリはサイズ0のカーソルを返しているため、インデックス0でカーソルにアクセスしようとしているため、エラーが発生します。 クエリを介して再リンクし、値のデータベースを確認してください。
おそらく0行カーソルにアクセスしています。に変更:
// moveToFirst itself will return false if there is no entry on Cursor
if(c.moveToFirst()){
int cursorCount = c.getCount();
int indexd = c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+index);
// You should getString(ColumnIndex) not CursorCount
return c.getString(indexd);
} else
return "";
. 列番号の代わりにレコード数を渡しているため、範囲外例外で索引を取得しています。 ベストコーディングプラクティスとして、レコードのカーソルを確認する、すなわち空にし、またはレコードが含まれています。 この
を使用してくださいString getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);
if(c.getCount()>0){
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT);
System.out.print("Count query"+index);
return c.getString(indexd);
}
else
return "";// when no records found
}
. 所属していません StackOverflow