Exception while reading from SQLite DB in android
I don't know the meaning of this error , anyone can help ?
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)
and ..
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)
this line of code that causes the problem ..
appointment.mStartDate = formatter.parse(c.getString(c.getColumnIndex("COL_START_DATE")));
Edit
more code .. The function that retrieve from database
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.getColumn开发者_Python百科Index("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;
}
piece of code that create the table ..
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 "+");");
You're requesting the column with the name COL_START_DATE
literally. That's not what you have. You should be doing:
c.getString(c.getColumnIndex(COL_START_DATE))
(same for the other ones)
I find it ..
I am passing the name of column as String , while it's a variable name !!
Thanks guys,
精彩评论