Multiple OrderBy in SQLiteDatabase.Query Method

I am using the SQLiteDatabase query 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, will the result set be sorted by FLD_CEEID in ascending or descending order?

Question 2: If I need to order the result set FLD_CEEID and then FLD_CEMID , how can I build an order on the parameter of this query.

Is it possible to execute several orders using this method?

+4
source share
2 answers

From SQLite Docs :

If the SELECT statement that returns more than one row does not have ORDER BY, the order in which the rows are returned is undefined. Or, if the SELECT statement has an ORDER BY clause, then the list of expressions attached to ORDER BY determines the order in which rows are returned to the user. Lines are first sorted based on the evaluation results of the leftmost expression in ORDER BY, then the links are broken, evaluating the second leftmost expression, etc. The order in which two rows for which all ORDER BY expressions for equal values ​​are returned undefined. Each ORDER BY expression may optionally be accompanied by one of the keywords ASC (lower values ​​are returned first) or DESC (larger values ​​are returned first). If neither ASC nor DESC is specified, the rows are sorted in ascending order (lower value) by default.

Answer 1: the 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); 
+5
source

It works like SQL Select Order By . You can find it here: http://en.wikipedia.org/wiki/Order_by_(SQL ).

0
source

Source: https://habr.com/ru/post/1433222/


All Articles