Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I order my SQLITE database in descending order, for an android app?

What is the most efficient method of showing my data in descending order?

public String getRank() {      String[] rank = new String[]{ KEY_ROWID };     Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, null);   //reading information from db.     String rankResult = "";      int iRow = c.getColumnIndex(KEY_ROWID); //Cursor looking for column setting equal to these ints.       for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {          //Move to first row - where cursor starts and moves to next row as long it is not after last row.         rankResult = rankResult + c.getString(iRow) + "\n";          //Returning value of row that it is currently on.     }     return rankResult;  //returning result }  public String getName() {      String[] name = new String[]{ KEY_NAME };     Cursor c = scoreDb.query(DATABASE_TABLE, name, null, null, null, null, null);   //reading information from db.     String nameResult = "";      int iRow1 = c.getColumnIndex(KEY_NAME); //Cursor looking for column setting equal to these ints.       for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {          //Move to first row - where cursor starts and moves to next row as long it is not after last row.         nameResult = nameResult + c.getString(iRow1) + "\n";          //Returning value of row that it is currently on.     }     return nameResult;  //returning result }  public String getScore() {      String[] score = new String[]{ KEY_SCORE };     Cursor c = scoreDb.query(DATABASE_TABLE, score, null, null, null,null, null);   //reading information from db.     String scoreResult = "";      int iRow2 = c.getColumnIndex(KEY_SCORE); //Cursor looking for column setting equal to these ints.       for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {          //Move to first row - where cursor starts and moves to next row as long it is not after last row.         scoreResult = scoreResult + c.getString(iRow2) + "\n";          //Returning value of row that it is currently on.     }     return scoreResult; //returning result } 
like image 378
Carla Dessi Avatar asked Jan 20 '12 22:01

Carla Dessi


People also ask

How do I sort descending in SQLite?

It allows you to sort the result set based on one or more columns in ascending or descending order. In this syntax, you place the column name by which you want to sort after the ORDER BY clause followed by the ASC or DESC keyword. The ASC keyword means ascending. And the DESC keyword means descending.

Which SQLite clause is used to sort the data in an ascending or descending order based on one or more columns?

SQLite ORDER BY clause is used to sort the data in an ascending or descending order, based on one or more columns.

Which keyboard is used for sorting the data in descending order in SQL?

Introduction to SQL ORDER BY clause To sort a result set in ascending order, you use ASC keyword, and in descending order, you use the DESC keyword.


1 Answers

Query has two syntax, the syntax you are using, last column represents orderBy, you just need to specify on what column you want to do orderBy +"ASC" (or) orderBy +"DESC"

Cursor c = scoreDb.query(DATABASE_TABLE, rank, null, null, null, null, yourColumn+" DESC");  

Refer this documentation to understand more about query() method.

like image 95
kosa Avatar answered Oct 16 '22 15:10

kosa