Question Detail

SQLite Order By - Sorting Result Set in DESCENDING ORDER | Android

3 years ago Views 813 Visit Post Reply

I am using SQLite in My Android Application for Store App Data in Local Database. I have fire below query to get all my table Data.

String selectQuery = "SELECT  * FROM " + TABLE_ALBUMS;


How can I get all Table Data in descending order? New data should be shown at the top of list 

Thread Reply

alex levine

- 3 years ago

Use this method in your code

to get descending order List

// Getting All Table Data
public List<DataClass> getAllData() {
    List<DataClass> dataList = new ArrayList<DataClass>();
    // Select All Data Query in descending order
    String selectQuery = "SELECT  * FROM " + TABLE_ALBUMS+ " ORDER BY " + KEY_ID + " DESC";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            DataClass album = new DataClass();
            // Adding album to list
        } while (cursor.moveToNext());
    // return Data list
    return dataList;