0

hey I m using cursor for query database by all mean but the is not pointing to any row i,e while debugging it show mrowId = null ,count =70 (what i inserted using content value is there) but when i write cursor.moveToNext() or cursor.moveToFirst(); it throws cursor index out of bound exception while cursor.moveToNext() or cursor.moveToFirst() always return in watch expression in eclipse ....... I hope you understand the problem help me in solving the issue...... thanks for your suggestion in advanced
for simplicity I am pasting code

public Cursor getDataBaseCursor(String tableName) {
    Cursor cursor = null;
    try {
        if (mainDatabase.isOpen()) {
            String countQuery = "SELECT  * FROM " + tableName;
            cursor = mainDatabase.rawQuery(countQuery, null);
            // cursor = mainDatabase.query(tableName, null, null, null,
            // null, null, null);
            if (cursor != null) {
                **boolean check =cursor.moveToNext();**
                return cursor;
            }

        } else {
            createOrOpenDatabase(databaseName);
            cursor = mainDatabase.query(tableName, null, null, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
                return cursor;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        cursor = mainDatabase.query(tableName, null, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToNext();
            return cursor;
        }

    }
    return null;

}

the boolean check is true while debugging

Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29

2 Answers2

0

may be this code will help you,

public Cursor getDataBaseCursor(String tableName) {
    Cursor cursor = null;
    try {
        if (mainDatabase.isOpen()) {
            String countQuery = "SELECT  * FROM " + tableName;
            cursor = mainDatabase.rawQuery(countQuery, null);

            int fieldValue; 
            if(cursor.getCount()>0){
                 cursor.moveToFirst();
                 do
             {
                    fieldValue=cursor.getInt(cursor.getColumnIndex("tbl_FieldName"));   
                    System.out.println("The Value of fatched field :"+fieldValue);
                 }while(cursor.moveToNext());
        cursor.close();
            }

          System.out.println("The Value of field :"+fieldValue);
       }
       catch (Exception e)
       {
     e.toString();
       }
user1208720
  • 515
  • 2
  • 6
0

Instead of

if (cursor != null) {
**boolean check =cursor.moveToNext();**
return cursor;

try

Log.d(TAG, "cursor.getCount()= " + String.valuseOf(cursor.getCount))
if(cursor.getCount > 0) {
    cursor.moveToFirst()
}
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • hey I am getting count at the time of debugging that i see in watch expression ok now when i nextstep the debugging it throws exception ,,might be it is because of the rowiD id null when i inspect the cursor object while debugging – Vipin Sahu May 10 '12 at 11:36
  • I could not understand where is problem and how can I help? – Gaurav Agarwal May 10 '12 at 11:38