How can I translate the following Orderby sql statement to
ORDER BY Country ASC, CustomerID DESC;
I have manage to write Ordering.property("Country").ascending();
but not sure how to add the next condition.
Thanks
If you look at the orderBy method, you will see that it has the following signature
public OrderBy orderBy(Ordering... orderings)
Note that you can put multiple Ordering objects inside.
Related
I'm trying to set up a user-selected SQL Query in Android Room.
#Query("SELECT * FROM wrestler_table ORDER BY :columnName :sortOrder LIMIT :size")
LiveData<List<WrestlersEntity>> getAllWrestlers(String columnName, String sortOrder, int size);
Android stuido is listing the sortOrder in the query as an error.
ASC, BETWEEN, COLLATE, DESC, IN, LIMIT, comma or semicolon expected, got ':sortOrder'
I want the sortOrder to be a variable so the user can change the order the data is returned in. Any thoughts on what I'm doing wrong here?
Well, you cannot pass the sort direction as query parameter. Not only SQLite, but most RDBMS do not allow this.
The following expression should do the trick:
ORDER BY
CASE WHEN :sortOrder = 'DESC' THEN :columnName END DESC,
CASE WHEN :sortOrder = 'ASC' THEN :columnName END ASC
LIMIT :size"
Alternatively, you can also concatenate the sort direction directly in the query string instead of passing it as a parameter.
I am developing an application when i retrieve a data from an sqlite database using rawQuery method because the query is a UNION query and the data is displayed on a listview . I would like to use a query method because i would also like to implement a content provider to handle the data retrieved from the database.
Is there any way someone can implement a union query using the query method.
I would only like to know of the structure since the query is already working using the rawQuery method how do i structure it using a query method
You can use following methods:
public String buildUnionQuery (String[] subQueries, String sortOrder, String limit)
public String buildQuery (String[] projectionIn, String selection, String groupBy, String having, String sortOrder, String limit)
For SQL queries for Union.
check the below links
http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
How to use rawQuery() in android
I've answered a similar question, take a look at my complete answer.
TL;DR: I think it doesn't currently exist any way to use a query instead of rawquery when you want to do a UNION query.
Is it possible to make two orderBy statements in one query. Say i wanna order my data after column 1 and afterwards after column 2. What would be the best way to do that?
Sincerely
Jesper
You just say ORDER BY Column1, Column2. If you are using the orderBy argument of one of the query methods, then remove the prefixing ORDER BY as it is inserted for you when building the query.
I want to do a rawquery and sort the result according to some alphanumeric value. Like I have column ItemId which contains values like A0001,A0002,A0036,B0085 etc.
String query="select S_PriceP,ItemId,ItemName from PDAProduct where ItemId<=A0001 and ItemId>=A0099 order by ItemId";
Is there any way I can achieve this?
You can use rawQuery.
String query="select S_PriceP,ItemId,ItemName from PDAProduct where ItemId<=A0001 and
ItemId>=A0099 order by ItemId";
Cursor objCursor = objSQLiteDatabase.rawQuery(query, null);
With the little info you have given us, my best answer would be to look at SQLiteDatabase docs... in particular, you want one of the query methods. If you were to post some of the code you have tried we might be able to find what you're missing.
I want to order my data objects from an ORMLite DAO case insensitively.
Currently I am using the following sqlite code to order my owner items case sensitively:
ownerDao.queryBuilder().orderBy("Name", true).query();
I see here that sqlite supports case insensitive "order by" with the following raw SQL:
SELECT * FROM owner ORDER BY Name COLLATE NOCASE
Any easy way (easier than calling queryRaw()) of adding the desired suffix?
Could an alternative solution be to set the columnDefinition property on the DatabaseField annotation to TEXT COLLATE NOCASE?
I think what you want is to use the QueryBuilder.orderByRaw(String) method. It would look something like:
ownerDao.queryBuilder().orderByRaw("Name COLLATE NOCASE").query();
Notice the raw string does not contain the ORDER BY part of the string. To quote the javadocs:
Add raw SQL "ORDER BY" clause to the SQL query statement. This should not include the "ORDER BY".
The TEXT COLLATE NOCASE columnDefinition might work although I don't have any experience with using that at all.
Also, I would not use the orderBy("Name COLLATE NOCASE", true) answer since typically ORMLite tries to escape the column names and the quotes would be in the wrong place.