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

后端 未结 11 1576
死守一世寂寞
死守一世寂寞 2020-11-27 16:44

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

public String getRank() {

    String[] rank = new String[]{ KEY_ROWID };
    Curso         


        
相关标签:
11条回答
  • 2020-11-27 17:19
    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 the record from database for list view in descending order. Move the cursor to last and move to previous record after each record is fetched. Hope this helps~

    0 讨论(0)
  • 2020-11-27 17:25

    According to docs:

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

    and your ORDER BY param means:

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

    So, your query will be:

     Cursor cursor = db.query(TABLE_NAME, null, null,
                null, null, null, KEY_ITEM + " DESC", null);
    
    0 讨论(0)
  • 2020-11-27 17:26
    return database.rawQuery("SELECT * FROM " + DbHandler.TABLE_ORDER_DETAIL +
                             " ORDER BY "+DbHandler.KEY_ORDER_CREATED_AT + " DESC"
                             , new String[] {});
    
    0 讨论(0)
  • 2020-11-27 17:26

    About efficient method. You can use CursorLoader. For example I included my action. And you must implement ContentProvider for your data base. https://developer.android.com/reference/android/content/ContentProvider.html

    If you implement this, you will call you data base very efficient.

    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 讨论(0)
  • 2020-11-27 17:27

    We have one more option to do order by

    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 this two method for order

    0 讨论(0)
  • 2020-11-27 17:30
    Cursor c = myDB.rawQuery("SELECT distinct p_name,p_price FROM products order by Id desc",new String[]{});
    

    this works for me!!!

    0 讨论(0)
提交回复
热议问题