从android中读取SQLite DB时出现异常

时间:2011-10-15 11:11:21

标签: java android sqlite

我不知道这个错误的含义,任何人都可以提供帮助吗?

10-15 13:07:02.221: DEBUG/query(587): SELECT event_id as _id,start_date, end_date, event_body, event_title, student_id, event_status, service_number, password, lecturer_id, fec_no, dept_no,floor_no from timetable
10-15 13:07:02.231: ERROR/CursorWindow(587): Bad request for field slot 1,-1. numRows = 4, numColumns = 13
10-15 13:07:02.231: DEBUG/AndroidRuntime(587): Shutting down VM
10-15 13:07:02.231: WARN/dalvikvm(587): threadid=1: thread exiting with uncaught exception (group=0x40015560)

和..

10-15 13:07:02.252: ERROR/AndroidRuntime(587): FATAL EXCEPTION: main
10-15 13:07:02.252: ERROR/AndroidRuntime(587): java.lang.IllegalStateException: get field slot from row 1 col -1 failed
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.database.CursorWindow.getString_native(Native Method)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.database.CursorWindow.getString(CursorWindow.java:329)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at com.android.database.DatabaseHelper.getAllAppointments(DatabaseHelper.java:337)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at com.android.uis.DaysActivity.bindList(DaysActivity.java:80)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at com.android.uis.DaysActivity.access$2(DaysActivity.java:78)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at com.android.uis.DaysActivity$2.onClick(DaysActivity.java:67)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.view.View.performClick(View.java:2485)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.view.View$PerformClick.run(View.java:9080)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.os.Handler.handleCallback(Handler.java:587)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.os.Looper.loop(Looper.java:130)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at java.lang.reflect.Method.invoke(Method.java:507)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-15 13:07:02.252: ERROR/AndroidRuntime(587):     at dalvik.system.NativeStart.main(Native Method)

导致问题的这行代码..

appointment.mStartDate = formatter.parse(c.getString(c.getColumnIndex("COL_START_DATE")));

修改

更多代码.. 从数据库中检索的函数

public ArrayList<Appointment> getAllAppointments(){
        ArrayList<Appointment> appointments = new ArrayList<Appointment>();
        Appointment appointment = null;
        SQLiteDatabase db = this.getWritableDatabase();

        String query = "SELECT "+
                COL_EVENT_ID+ " as _id,"+
                COL_START_DATE+", "+
                COL_END_DATE+", "+
                COL_EVENT_BODY+", "+
                COL_EVENT_TITLE+", "+
                COL_STUDENT_ID+", "+    
                COL_EVENT_STATUS+", "+
                COL_SERVICE_NUMBER+", "+
                COL_PASSWORD+", "+
                COL_LECTURER_ID+", "+       
                COL_SEC_NO+", "+
                COL_DEPT_NO+","+
                COL_FLOOR_NO+" from "+TIMETABLE_TABLE;

        Cursor c = db.rawQuery(query,new String[]{});
        Log.d("query",query);

        c.moveToFirst();
        while(c.moveToNext()){
            appointment = new Appointment();
            try {
                appointment.mEventId = c.getInt(c.getColumnIndex("_id"));
                appointment.mStartDate = formatter.parse(c.getString(c.getColumnIndex("COL_START_DATE")));
                appointment.mEndDate = formatter.parse(c.getString(c.getColumnIndex("COL_END_DATE")));
                appointment.mBody = c.getString(c.getColumnIndex("COL_EVENT_BODY"));
                appointment.mTitle = c.getString(c.getColumnIndex("COL_EVENT_TITLE"));
                appointment.mStudentId = c.getString(c.getColumnIndex("COL_STUDENT_ID"));
                appointment.mSatus = c.getInt(c.getColumnIndex("COL_EVENT_STATUS"));
                appointment.mServiceNo = c.getInt(c.getColumnIndex("COL_SERVICE_NUMBER"));
                appointment.mPassword = c.getString(c.getColumnIndex("COL_PASSWORD"));
                appointment.mLecturerId = c.getInt(c.getColumnIndex("COL_LECTURER_ID"));
                appointment.mSecNo = c.getInt(c.getColumnIndex("COL_SEC_NO"));
                appointment.mDeptNo = c.getInt(c.getColumnIndex("COL_DEPT_NO"));
                appointment.mFloorNo = c.getInt(c.getColumnIndex("COL_FLOOR_NO"));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            appointments.add(appointment);
        }
        db.close();
        return appointments;
    }

创建表的代码..

db.execSQL("CREATE TABLE "+ TIMETABLE_TABLE+
                " ("+COL_EVENT_ID+" TEXT PRIMARY KEY , "+
                COL_START_DATE+ " TEXT , "+
                COL_END_DATE+" TEXT , "+
                COL_EVENT_BODY+" TEXT ,"+
                COL_STUDENT_ID+ " TEXT , "+
                COL_EVENT_TITLE+ " TEXT , "+
                COL_EVENT_STATUS+" INTEGER ,"+
                COL_SERVICE_NUMBER+ " INTEGER , "+
                COL_PASSWORD+" TEXT , "+
                COL_LECTURER_ID+" TEXT ,"+
                COL_SEC_NO+ " INTEGER , "+
                COL_DEPT_NO+" INTEGER , "+
                COL_FLOOR_NO+" INTEGER "+");");

2 个答案:

答案 0 :(得分:0)

我找到了..

我将列的名称作为String传递,而它是一个变量名!!

谢谢你们,

答案 1 :(得分:0)

您正在逐字地请求名称为COL_START_DATE的列。那不是你拥有的。你应该这样做:

c.getString(c.getColumnIndex(COL_START_DATE))

(与其他人相同)