开发者

SQLite Query in non case sensitive alphabetical order [duplicate]

开发者 https://www.devze.com 2023-03-05 10:55 出处:网络
This question already has answers here: How to use SQL Order By statement to sort results case insensitive?
This question already has answers here: How to use SQL Order By statement to sort results case insensitive? 开发者_如何转开发 (3 answers) Closed 6 years ago.

All I want to do is grab the stuff in alphabetical order and ignore the capital letters.

db.rawQuery("SELECT " + catName + " FROM "+tableName+" ORDER BY "+catName+" ASC COLLATE NOCASE;", null);

This is the code I'm using above, but it always gives me an SQLite exception saying that COLLATE is a syntax error.

android.database.sqlite.SQLiteException: near "COLLATE": syntax error: , while compiling: SELECT Artist FROM testTable COLLATE NOCASE ASC


COLLATE goes before the order direction:

db.rawQuery("SELECT " + catName 
           + " FROM " +tableName 
        +" ORDER BY "+catName+" COLLATE NOCASE ASC;", null);

But you don't need the ASC -- that's the default so you could just as well use:

db.rawQuery("SELECT "+ catName 
            +" FROM "+ tableName 
        +" ORDER BY "+ catName +" COLLATE NOCASE;", null);


add COLLATE NOCASE after orderBy String.

db.query(table, columns, selection,
        selectionArgs, groupBy, having,
        orderBy + " COLLATE NOCASE ASC");

here, order by ASC or DESC depends on your need.


This should work too I think:

db.rawQuery("SELECT "+ catName 
        +" FROM "+ tableName 
    +" ORDER BY lower("+ catName +");", null);
0

精彩评论

暂无评论...
验证码 换一张
取 消