Android SQLLiteDataBase cursor returning 0 rows

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!

+4
source share
2 answers

Nothing seems random here at all. It seems that you are looking for column_a with a value of 0 each time.

I would suggest that column_a has nothing with a value of 0.

+2
source

First of all, try to request this method:

 String args[] = new String[]{"0"}; Cursor cursor = db.rawQuery( "SELECT * FROM " + DATABASE_TABLE + " WHERE " + COLUMN_A + " = ?", args); 

If this does not help check your db with an external tool, if you are requesting return strings there.

+2
source

All Articles