Aakaash Jois
Aakaash Jois

Reputation: 288

Ordering in DBFLow

I am trying to implement DBFlow in my Android Project. I have been able to create a table and add rows and columns to it. I have two columns, ID and Names. Now I want to retrieve the Names from the table and arrange it in descending order by ID. I found this example syntax in Github DBFlow page

SQLite.select()
  .from(table)
  .where()
  .orderBy(Customer_Table.customer_id, true)
  .queryList();

SQLite.select()
    .from(table)
    .where()
    .orderBy(Customer_Table.customer_id, true)
    .orderBy(Customer_Table.name, false)
    .queryList();

But I am unable to under what "Customer_Table.customer_id" means in that code. When I try to put the method orderBy(), Android Studio suggests (NameAlias nameAlias, boolean) as the parameter. How do I added a NameAlias to my table? Can someone please help me with this?

Upvotes: 3

Views: 3073

Answers (2)

Aldrin Joe Mathew
Aldrin Joe Mathew

Reputation: 482

When you implement DbFlow in your project it automatically builds a table class for each table model you have annotated with the @Table annotation.

i.e, if you have a table with name Customer then the corresponding class would be Customer_Table and if your table is User then the corresponding class would be User_Table.

Let this be your Customer table:

@Table
public class Customer extends BaseModel {
    @Column
    String customer_name;
}

If you want to order your results with customer name in Customer table, then use like this,

SQLite.select()
  .from(table)
  .where()
  .orderBy(Customer_Table.customer_name, true)
  .queryList();

Upvotes: 3

Ashim Kansal
Ashim Kansal

Reputation: 263

In "Customer_Table.customer_id", Customer_Table is name of table and customer_id is column name. You can get the name and id from database and sort the data list programmatically or you can use this query

mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_NAME}, null, null, null, null, KEY_PID+" DESC", null);

Upvotes: 0

Related Questions