尽管插入了,但select命令不会从SQLite中检索数据?

时间:2011-10-19 10:26:20

标签: android sqlite

我使用follow命令插入数据,并通过插入命令获取true ..

 public boolean insertAppointment(Appointment appointment) {
        SQLiteDatabase db=this.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_EVENT_ID, appointment.mEventId);
        values.put(COL_START_DATE, formatter.format(appointment.mStartDate));

    //  Log.d("date when insert",appointment.mStartDate+"");

        values.put(COL_END_DATE, formatter.format(appointment.mEndDate));

        values.put(COL_EVENT_BODY, appointment.mBody);        
        values.put(COL_EVENT_TITLE, appointment.mTitle);
        values.put(COL_STUDENT_ID, appointment.mStudentId);        
        values.put(COL_EVENT_STATUS, appointment.mSatus);
        values.put(COL_SERVICE_NUMBER, appointment.mServiceNo);        
        values.put(COL_PASSWORD, appointment.mPassword);
        values.put(COL_LECTURER_ID, appointment.mLecturerId);        
        values.put(COL_SEC_NO, (Integer)appointment.mSecNo);
        values.put(COL_DEPT_NO, (Integer)appointment.mDeptNo);
        values.put(COL_FLOOR_NO, (Integer)appointment.mFloorNo);
        boolean r = db.insert(TIMETABLE_TABLE, null, values) > 0 ;

        Log.d("db:appointments rows inserted",r+"");
        Log.d("db:appointments rows after insert",this.getAllAppointments().size()+"");

        db.close();
        return r;
    }

但是在尝试使用以下命令检索数据时, 我没有得到任何结果!?为什么?

编辑

以下是负责检索数据的函数

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;
    Log.d("select query",query);
    Cursor c = db.rawQuery(query,new String[]{});
    Log.d("cursor",c.getCount()+"");

    c.moveToFirst();
    while(c.moveToNext()){
        appointment = new Appointment();
        try {
            appointment.mEventId = c.getInt(c.getColumnIndex("_id"));

            Date startDate = formatter.parse(formatter.format(formatter.parse(c.getString(c.getColumnIndex(COL_START_DATE)))));
            appointment.mStartDate = startDate;

            Date endtDate = formatter.parse(formatter.format(formatter.parse(c.getString(c.getColumnIndex(COL_END_DATE)))));
            appointment.mEndDate = endtDate; 

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

1 个答案:

答案 0 :(得分:2)

试试这个,,,

在插入值时使用SQLiteDatabase db = this.getWritableDatabase();

在阅读值时使用SQLiteDatabase db=this.getReadableDatabase();

你正在反过来使用它们。