dimanche 21 juin 2015

Why doesn't my SQLite cursor return in alphabetical order? (Android)

I have been trying to return my table using db.query in alphabetical order based on the name and save as a List for to populate a spinner.

The db.query does successfully return all the column values, but doesn't return in any order when I try sort.

The column I want to sort by is COLUMN_COMPANY_NAME ("company_name") belong to table TABLE__NAME("companies"). COLUMN_COMPANY_NAME was declared as:

private static final String DATABASE_CREATE = "create table " + TABLE_NAME
+ "(" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_COMPANY_NAME + " varchar(45), "
+ COLUMN_EMAIL + " varchar(45), "
+ COLUMN_PHONE + " integer default 0"
+ ");";

private SQLiteDatabase db;

I was wanting to use db.query(table, columns, selection, selectionArgs, groupBy, having order) to get all the columns and rows in the database in a cursor:

protected Cursor Cursor_getAllCompanies(){
    String orderBy = COLUMN_COMPANY_NAME + " Collate NOCASE";
    return db.query(TABLE_NAME, null, null, null, null, null, orderBy);
}//if columns = null, default get all columns

So I would expect the rawQuery to be:

SELECT * FROM companies ORDER BY company_name Collate NOCASE;

I have tried a number of different syntaxes but to no avail. As far as I know the above syntax should return all my columns in (ascending) alphabetical order base on company name, but doesn't.

The question boils down to: How can I get the db.query to return in an alphabetised order? And why isn't standard order command working?

Note: There are a number of similar questions that have been answered before, but none of their solutions currently appear to work for me:

SQLite Query, Nocase Alphabetical

How do I order my SQLite in descending order

SQLite query sorting

Aucun commentaire:

Enregistrer un commentaire