SQLite Query in non case sensitive alphabetical order [duplicate]

浪尽此生 提交于 2019-12-17 22:07:41

问题


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


回答1:


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



回答2:


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.




回答3:


This should work too I think:

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


来源:https://stackoverflow.com/questions/5958039/sqlite-query-in-non-case-sensitive-alphabetical-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!