Throughout life, I cannot make the cursor return any data. I checked that this is data in the database and my inserts work. Official mistake:
CursorIndexOutOfBoundsException: requested index 0, with size 0
Top Level Ads:
private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); this.db = this.DBHelper.getWritableDatabase(); }
My function:
public String getRandomEntry() { int rand; Random random = new Random(); int numEntries = (int)this.getCount(); if(numEntries == 0) return "ERROR: Database is empty."; rand = random.nextInt(numEntries); Cursor cursor = DBHelper.getWritableDatabase().rawQuery( "SELECT * FROM " + DATABASE_TABLE + " WHERE " + COLUMN_A + " = " + 0, null); Log.i("numEntries", Integer.toString(numEntries)); Log.i("rand", Integer.toString(rand)); Log.i("cursor", Integer.toString(cursor.getCount())); cursor.moveToFirst(); return cursor.getString(0); }
I also tried to grab the cursor as such:
Cursor cursor = db.rawQuery( "SELECT * FROM " + DATABASE_TABLE + " WHERE " + COLUMN_A + " = " + 0, null);
Please give me your thoughts! Thanks!
source share