1

我正在处理我想从 Sqlite 数据库表中获取数据的 android 应用程序。

即使数据存在于表中,它也会给我错误。

代码:

 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;
        }

错误线:

int mid=cursor.getInt(1);

日志猫:

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

请帮助我,我不明白错误的原因。

我已确保数据存在于表中。

在此处输入图像描述

4

2 回答 2

2

只需更改以下内容:

if (rows>0) {

cursor.moveToFirst();

do{

 // get data here from database...

}while(cursor.moveToNext());

}

而且当你的光标第一次移动到第一个位置时,它不为空并且它的计数必须> 0 ..并且该值必须从0开始而不是从1开始......所以改变

int mid=cursor.getInt(0);
于 2013-09-17T06:40:04.140 回答
1

数据库中只有一个值,因此将其读取为 0 索引而不是 1 并且游标从 0 开始。使用 cursor.moveTofirst();

int mid=cursor.getInt(0);
于 2013-09-17T06:44:02.340 回答