Bader
Bader

Reputation: 3774

select command doesn't retrieve data from SQLite despite it is inserted?

I insert data using the follow command and getting true as a result of insert command ..

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

But when trying to retrieve data using the following command, I don't get any result !? Why ?

edit

Here is the the function that is responsible for retrieving data

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

Upvotes: 0

Views: 265

Answers (1)

Noby
Noby

Reputation: 6602

try this,,,

use SQLiteDatabase db = this.getWritableDatabase(); when inserting the values.

use SQLiteDatabase db=this.getReadableDatabase(); when reading the values.

you are using them in reverse.

Upvotes: 2

Related Questions