ORDER BY Date DESC时出现问题

时间:2017-09-11 14:11:03

标签: android sql android-sqlite

我创建了一个android sql数据库,开始时每件事都很好。

但是我发现一个问题就是有人在2017/9/8而不是2017/9/08进入单日,我的order by date desc无效。

这是我的表,我想按日期对行进行排序: enter image description here

当这一天是2017/8/7和2017/9/7时,这是行不通的

enter image description here

这是我的排序日期代码: //排序日期:最新到旧日期

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

看起来当天数是单一会导致问题,我试图通过其他人订购,他们没有工作,我不知道如何弄明白。

任何帮助将不胜感激。提前谢谢。

我创建了数据库,TEXT是日期类型:

@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);

}

3 个答案:

答案 0 :(得分:2)

警告:您将要看到的SQLite查询很难看,不应该在家中重复,除非您完全搞砸了您的日期格式,在这种情况下您可能别无选择。

我们可以尝试执行ORDER BY,为每个日期构建正确的两位数月份和日期。请注意,对于您的数据,这需要将单个数字的月份和日期填充为零。

SELECT *
FROM contacts
ORDER BY
    SUBSTR(Date, 1, 4) DESC,
    CASE WHEN INSTR(SUBSTR(Date, 6), '/') = 2
         THEN '0' || SUBSTR(Date, 6, 1)
         ELSE SUBSTR(Date, 6, 2) END DESC,
    CASE WHEN LENGTH(SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1)) = 1
         THEN '0' || SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1)
         ELSE SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1) END DESC;

请注意,正确的长期解决方案是您将所有月份和日期存储为两位数字,在单个数字的情况下,左侧填充零(您的年份很可能总是4位数)。 / p>

以下是演示的链接,演示了上述查询的逻辑。请注意,我使用MySQL创建它,因为不支持SQLite,但除了必须用||替换CONCAT之外,查询是相同的。

Demo

以下输出显示正确的顺序以及正确提取的年,月和日组件:

enter image description here

答案 1 :(得分:1)

我认为因为你按字符串值排序......

尝试类似ORDER BY CAST(Date as DATE) DESC

的内容

答案 2 :(得分:1)

在没有任何排序的情况下获取所有data,然后使用自定义List

comparator上执行排序
class DateComparator implements Comparator<Contact>
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    public int compare(Contact lhs, Contact rhs)
    {
        return dateFormat.parse(lhs.getDate()).compareTo(dateFormat.parse(rhs.getDate()));
    }
}

使用上面的比较器按日期排序

List<Contact> contactList = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS,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());
    }

Collections.sort(contactList, new DateComparator());