문제

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 "";
.

빈 ResultSet에서 읽으려고하는 것과 같습니다.

데이터베이스를 확인하면 쿼리가 크기 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
}
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top