How can I order a SQLITE database in descending order for an Android app?

What is the most efficient way to display 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 } 
+49
android sqlite sql-order-by
Jan 20 '12 at 22:01
source share
10 answers

Query has two syntaxes, the syntax you use, the last column represents the order, you just need to specify in which column you want to make orderby + "ASC" (or) orderby + "DESC"

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

See the this documentation for more information on the request method.

+149
Jan 20 2018-12-12T00:
source share
 Cursor c = scoreDb.query(Table_Name, score, null, null, null, null, Column+" DESC"); 

try it

+12
Nov 20 '13 at 10:10
source share
 return database.rawQuery("SELECT * FROM " + DbHandler.TABLE_ORDER_DETAIL + " ORDER BY "+DbHandler.KEY_ORDER_CREATED_AT + " DESC" , new String[] {}); 
+10
Sep 04 '14 at 14:22
source share

According to the docs :

 public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit); 

and your ORDER BY means:

How to arrange rows formatted as an SQL ORDER BY clause (with the exception of ORDER BY itself). Passing null will use the default sort order, which may be unordered.

So your query will look like this:

  Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, KEY_ITEM + " DESC", null); 
+3
Oct 16 '15 at 8:26
source share
 Cursor c = myDB.rawQuery("SELECT distinct p_name,p_price FROM products order by Id desc",new String[]{}); 

it works for me !!!

+3
Jul 05 '16 at 7:10
source share

you can do it with this

 Cursor cursor = database.query( TABLE_NAME, YOUR_COLUMNS, null, null, null, null, COLUMN_INTEREST+" DESC"); 
+2
May 19 '16 at 19:08
source share
 public List getExpensesList(){ SQLiteDatabase db = this.getWritableDatabase(); List<String> expenses_list = new ArrayList<String>(); String selectQuery = "SELECT * FROM " + TABLE_NAME ; Cursor cursor = db.rawQuery(selectQuery, null); try{ if (cursor.moveToLast()) { do{ String info = cursor.getString(cursor.getColumnIndex(KEY_DESCRIPTION)); expenses_list.add(info); }while (cursor.moveToPrevious()); } }finally{ cursor.close(); } return expenses_list; } 

This is my way of reading a record from a database to view the list in descending order. Move the cursor to the last one and go to the previous record after each record. Hope this helps ~

+2
Mar 02 '17 at 9:37 on
source share

We have another way to place an order.

 public Cursor getlistbyrank(String rank) { try { //This can be used return db.`query("tablename", null, null, null, null, null, rank +"DESC",null ); OR return db.rawQuery("SELECT * FROM table order by rank", null); } catch (SQLException sqle) { Log.e("Exception on query:-", "" + sqle.getMessage()); return null; } } 

You can use these two methods to order.

+1
May 11 '16 at 7:58 a.m.
source share

About an effective method. You can use CursorLoader. For example, I turned on my action. And you must implement ContentProvider for your database. https://developer.android.com/reference/android/content/ContentProvider.html

If you implement this, you will very effectively name your database.

 public class LoadEntitiesActionImp implements LoaderManager.LoaderCallbacks<Cursor> { public interface OnLoadEntities { void onSuccessLoadEntities(List<Entities> entitiesList); } private OnLoadEntities onLoadEntities; private final Context context; private final LoaderManager loaderManager; public LoadEntitiesActionImp(Context context, LoaderManager loaderManager) { this.context = context; this.loaderManager = loaderManager; } public void setCallback(OnLoadEntities onLoadEntities) { this.onLoadEntities = onLoadEntities; } public void loadEntities() { loaderManager.initLoader(LOADER_ID, null, this); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader(context, YOUR_URI, null, YOUR_SELECTION, YOUR_ARGUMENTS_FOR_SELECTION, YOUR_SORT_ORDER); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { } @Override public void onLoaderReset(Loader<Cursor> loader) { } 
0
Nov 11 '16 at 9:59
source share

This is a terrible thing! It costs me a few hours! these are my table rows:

 private String USER_ID = "user_id"; private String REMEMBER_UN = "remember_un"; private String REMEMBER_PWD = "remember_pwd"; private String HEAD_URL = "head_url"; private String USER_NAME = "user_name"; private String USER_PPU = "user_ppu"; private String CURRENT_TIME = "current_time"; Cursor c = db.rawQuery("SELECT * FROM " + TABLE +" ORDER BY " + CURRENT_TIME + " DESC",null); 

Each time I update the table, I will update CURRENT_TIME for sorting. But I found that this is not work. The result is not sorted what I want. Finally, I found that the column "current_time" is the default row for sqlite. The solution is to rename the column "cur_time" instead of "current_time".

0
Dec 10 '16 at 7:34
source share



All Articles