为什么我的SQLite游标不按字母顺序返回? (机器人)

时间:2022-05-26 20:20:04

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.

我一直在尝试使用db.query按名称的字母顺序返回我的表,并保存为List以填充微调器。

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

db.query成功返回所有列值,但在尝试排序时不以任何顺序返回。

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:

我要排序的列是COLUMN_COMPANY_NAME(“company_name”)属于表TABLE__NAME(“公司”)。 COLUMN_COMPANY_NAME被声明为:

public static final String TABLE_NAME = "companies";
public static final String COLUMN_ID = "company_id";
public static final String COLUMN_COMPANY_NAME = "company_name";
public static final String COLUMN_EMAIL = "company_email";
public static final String COLUMN_PHONE = "company_phone";

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:

我想使用db.query(table,columns,selection,selectionArgs,groupBy,具有顺序)来获取游标中数据库中的所有列和行:

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:

所以我希望rawQuery是:

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?

问题归结为:如何让db.query以字母顺序返回?为什么标准订单命令不起作用?

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

SQLite查询,Nocase按字母顺序排列

How do I order my SQLite in descending order

如何按降序排列SQLite

SQLite query sorting

SQLite查询排序

1 个解决方案

#1


0  

if your trying to list it in alphabetical order
just SELECT * FROM companies ORDER BY company_name

如果你试图按字母顺序列出它只是SELECT * FROM公司ORDER BY company_name

#1


0  

if your trying to list it in alphabetical order
just SELECT * FROM companies ORDER BY company_name

如果你试图按字母顺序列出它只是SELECT * FROM公司ORDER BY company_name