1

I working on android application in which i wanted to get data from Sqlite database table.

Even if the data is present in the table its giving me error.

Code:

 public List<Mikats> getAllMikats() {
            List<Mikats> contactList = new ArrayList<Mikats>();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_NAME_2;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            //cursor.moveToFirst()
            int rows=cursor.getCount();
            if (rows>0) {
                do {
                    Mikats mikat = new Mikats();

                    int mid=cursor.getInt(1);
                    String sqlectEventTitle="select * from "+ TABLE_NAME_1 +" where "+ KEY_MID+" = "+mid;
                    SQLiteDatabase db2 = this.getWritableDatabase();
                    Cursor cursor1=db2.rawQuery(sqlectEventTitle, null);
                    if(cursor1.moveToNext())
                    {
                        String Title=cursor1.getString(cursor1.getColumnIndex("EventTitle"));
                        mikat.setEventTitle(Title);
                    }
                    mikat.setEjamatID(Integer.parseInt(cursor.getString(0)));

                    mikat.setMikatID(Integer.parseInt(cursor.getString(1)));

                    mikat.setStartDate(cursor.getString(2));

                    mikat.setEndDate(cursor.getString(3));

                    mikat.setBlock(cursor.getString(4));


                    mikat.setFloor(cursor.getString(5));

                    mikat.setGate(cursor.getString(6));

                    String masjid=cursor.getString(7);
                    mikat.setMasjid(masjid);

                    mikat.setAutoID(Integer.parseInt(cursor.getString(8)));

                    mikat.setSeatNo(cursor.getString(9));

                    // Adding contact to list
                    contactList.add(mikat);
                } while (cursor.moveToNext());
            }

            // return contact list
            return contactList;
        }

Line of Error:

int mid=cursor.getInt(1);

Logcat:

09-17 11:51:57.904: W/System.err(19441): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
09-17 11:51:57.904: W/System.err(19441):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09-17 11:51:57.904: W/System.err(19441):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09-17 11:51:57.914: W/System.err(19441):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09-17 11:51:57.924: W/System.err(19441):    at com.example.busahebaapp.MikatHandler.getAllMikats(MikatHandler.java:113)
09-17 11:51:57.924: W/System.err(19441):    at com.example.busahebaapp.ViewEvents.onCreate(ViewEvents.java:57)
09-17 11:51:57.934: W/System.err(19441):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-17 11:51:57.944: W/System.err(19441):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-17 11:51:57.944: W/System.err(19441):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-17 11:51:57.944: W/System.err(19441):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-17 11:51:57.954: W/System.err(19441):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-17 11:51:57.964: W/System.err(19441):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 11:51:57.974: W/System.err(19441):    at android.os.Looper.loop(Looper.java:123)
09-17 11:51:57.974: W/System.err(19441):    at android.app.ActivityThread.main(ActivityThread.java:3683)
09-17 11:51:57.984: W/System.err(19441):    at java.lang.reflect.Method.invokeNative(Native Method)
09-17 11:51:57.994: W/System.err(19441):    at java.lang.reflect.Method.invoke(Method.java:507)
09-17 11:51:57.994: W/System.err(19441):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-17 11:51:58.004: W/System.err(19441):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-17 11:51:58.004: W/System.err(19441):    at dalvik.system.NativeStart.main(Native Method)
09-17 11:53:24.805: W/jdwp(19441): Debugger is telling the VM to exit with code=1

Please help me i am not understanding cause of error.

I have made sure that data is present in the table.

enter image description here

5
  • Why have you commented //cursor.moveToFirst()? Commented Sep 17, 2013 at 6:38
  • it was wrong for my logic Commented Sep 17, 2013 at 6:39
  • You should check if (cursor != null && cursor.moveToFirst()) ... do {} while(); Commented Sep 17, 2013 at 6:40
  • @Tarun thanx sir...every time when i need to fetch the value i should write this? Commented Sep 17, 2013 at 6:44
  • +1 for figuring out that you can deal with your SQLite database on the desktop. So many people don't grok that. Commented Sep 17, 2013 at 6:47

2 Answers 2

2

Just change with this:

if (rows>0) {

cursor.moveToFirst();

do{

 // get data here from database...

}while(cursor.moveToNext());

}

And also when first time your cursor move to First position when it is not null and its count must be >0.. and the value must be started with 0 not from 1... So change

int mid=cursor.getInt(0);
Sign up to request clarification or add additional context in comments.

3 Comments

everytime when i want to fetch the value i should make it as ursor.moveTofirst(); ???
whenever you fetch data from database call cursor.moveToFirst() before do while loop and its count must be >0
@ShrimantBajiraoPeshawe-I It depends what you want to do. moveToFirst(), er, moves to the first row.
1

there is only one value in database so read it as 0 index not 1 and cursor starts from 0. use cursor.moveTofirst();

int mid=cursor.getInt(0);

1 Comment

everytime when i want to fetch the value i should make it as ursor.moveTofirst(); ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.