I create a android sql database , every thing is fine at beginning.
But i find an issue that is someone enter a single day like 2017/9/8 not 2017/9/08 , my order by date desc
is not working.
Here is my table , i want to sort the row by Date:
It is no working when the day is 2017/8/7 and 2017/9/7
Here is my sort date code: //Sort date : Latest to Old date
public List<Contact> sortingDate() {
List<Contact> contactList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS +" ORDER BY " +"Date DESC",null);
//looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setDate(cursor.getString(1));
contact.setBeforeMorning(cursor.getString(2));
contact.setAfterMorning(cursor.getString(3));
contact.setBeforeNoon(cursor.getString(4));
contact.setAfterNoon(cursor.getString(5));
contact.setBeforeNight(cursor.getString(6));
contact.setAfterNight(cursor.getString(7));
System.out.println("The result is :" + cursor.getString(1));
//Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
It looks like when the day number is single will cause the issue, i try to order by orthers , they are no working , i don't know how to figure it out.
Any help would be appreciated . Thanks in advance.
I create the database, TEXT is date type:
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("table is here");
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
+ KEY_BMORNING + " TEXT," + KEY_AMORNING + " TEXT,"
+ KEY_BNOON + " TEXT," + KEY_ANOON + " TEXT,"
+ KEY_BNIGHT + " TEXT," + KEY_ANIGHT + " TEXT" +
")";
db.execSQL(CREATE_CONTACTS_TABLE);
}