SQLiteQueryBuilder反向排序顺序

时间:2011-06-05 10:44:56

标签: android sqlite

我使用ContentProvider和SQLiteQueryBuilder

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(NEWS_TABLE);

        switch(sUriMatcher.match(uri)){
          case NEWS_ITEM_ID: qb.appendWhere(KEY_ID+ "=" + uri.getPathSegments().get(1));
                           break;
          default: break;
        }

        String orderBy;

        if (TextUtils.isEmpty(sortOrder)) {
              orderBy = KEY_DATE;
            } else {
                /* ______  WANT TO REVERSE IT HERE _____ */
              orderBy = sortOrder;
            }
        Cursor c = qb.query(newsDB, 
                            projection, 
                            selection, selectionArgs, 
                            null, null, 
                            orderBy);

        c.setNotificationUri(getContext().getContentResolver(), uri);

        return c;
    }

如何使用反向排序顺序进行查询?谢谢你的帮助。

1 个答案:

答案 0 :(得分:15)

SQL中的

ORDER BY子句可能有升序或降序。默认情况下使用升序。要设置反向排序顺序,您需要在DESC子句的末尾添加ORDER BY关键字。

if (TextUtils.isEmpty(sortOrder)) {
    orderBy = KEY_DATE;
} else {
    /* ______  WANT TO REVERSE IT HERE _____ */
    orderBy = KEY_DATE + " DESC";
}