Multiple OrderBy in SQLiteDatabase.Query Method

I am using the query method of SQLiteDatabase. I need help with the orderBy parameter of this method.

Cursor cursor = sqLiteDatabase.query(tableName, tableColumns, whereClause, whereArgs, groupBy, having, orderBy);

public Cursor getAllCustomexp(int TID) throws SQLException 
{
    Cursor mCursor =
            db.query(false, CEXP_TABLE, new String[] {KEY_CEID, FLD_CETID, FLD_CEEID, FLD_CEMID,
            FLD_CEAMT, FLD_CESEL}, FLD_CETID + " = " + TID, null,
            null, null, FLD_CEEID, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

Question 1: In the above query the result set will be sorted by FLD_CEEID in ascending or descending order ?

Question 2: If I need to order the result set first by FLD_CEEID and then by FLD_CEMID how should i construct the order by parameter of this query.

Is it possible to do multiple order by using this method?

Answers


From SQLite docs:

If a SELECT statement that returns more than one row does not have an ORDER BY clause, the order in which the rows are returned is undefined. Or, if a SELECT statement does have an ORDER BY clause, then the list of expressions attached to the ORDER BY determine the order in which rows are returned to the user. Rows are first sorted based on the results of evaluating the left-most expression in the ORDER BY list, then ties are broken by evaluating the second left-most expression and so on. The order in which two rows for which all ORDER BY expressions evaluate to equal values are returned is undefined. Each ORDER BY expression may be optionally followed by one of the keywords ASC (smaller values are returned first) or DESC (larger values are returned first). If neither ASC or DESC are specified, rows are sorted in ascending (smaller values first) order by default.

Answer 1: Result set will be sorted in ascending order.

Answer 2:

String orderBy = FLD_CEEID + " ASC, " + FLD_CEMID + " ASC";
db.query(false, CEXP_TABLE, new String[] {KEY_CEID, FLD_CETID, FLD_CEEID, FLD_CEMID,
        FLD_CEAMT, FLD_CESEL}, FLD_CETID + " = " + TID, null,
        null, null, orderBy, null);

Need Your Help

data import ideas

java design web-applications java-ee

I have developed a system where the admin user is expected to update the approval of xyz from another website which has the details. This is being done manually for many records. I am planning to

C++ 3D vector preserving “blocks”

c++ vector multidimensional-array stdvector preserve

Suppose I require an undetermined number of 3-by-4 matrices. (Or a sequence of any other fixed m-by-n-dimensional matrices.) My first thought is to store these matrices in a std::vector, where each