Reputation: 382
I'm having a problem when using the Cursors to perform a SQLite query to an Android database, if I try to use getPage I get an android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
error, am I doing something wrong with the cursors? doing a cursor.getColumnCount() gets 4 that's correct, and there's a row, so that's not the problem, what could be causing the error? Thanks in advance
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "pagesManager";
// Pages table name
private static final String TABLE_PAGES = "pages";
// Pages Table Columns names
private static final String PAGES_KEY_ID = "id";
private static final String PAGES_KEY_NAME = "pageName";
private static final String PAGES_KEY_DATE = "lastDate";
private static final String PAGES_KEY_PAGE = "pageUrl";
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_PAGES_TABLE = "CREATE TABLE " + TABLE_PAGES + "("
+ PAGES_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + PAGES_KEY_NAME + " TEXT,"+ PAGES_KEY_DATE +
" TEXT," + PAGES_KEY_PAGE + " TEXT" +")";
db.execSQL(CREATE_PAGES_TABLE);
}
public PageListItem getPage(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PAGES, new String[] { PAGES_KEY_ID, PAGES_KEY_NAME, PAGES_KEY_DATE, PAGES_KEY_PAGE }, PAGES_KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
if(cursor.moveToFirst()) {
System.out.println("Cursor no es nulo");
}
PageListItem page = new PageListItem(cursor.getString(1),cursor.getString(2), cursor.getString(3),Integer.parseInt(cursor.getString(0)));
cursor.close();
return page;
}
}
Upvotes: 1
Views: 230