I want to make one query like this:
SELECT * FROM my_table where column_one <= column_two;
With QueryBuilder I can to make where().le(column_one, Object obj), but I want some like where().le(column_one, column_two);
Actually, I want the following query:
SELECT * FROM table_one INNER JOIN table_two ON
table_one.column_foreign_id = table_two.id WHERE table_two.column_one
<= table_two.column_two.
What is the best way?
Thank you for your time.
Yes, you can do it.
Code:
QueryBuilder<Account, String> queryBuilder = accountDao.queryBuilder();
queryBuilder.where().le(Account.COLUMN_ONE_NAME,
new ColumnArg(Account.COLUMN_TWO_NAME));
List<Account> results = queryBuilder.query();
More information here: see 3.7 Using Column Arguments
Have you considered using rawQuery instead?
As the docs say:
The built-in methods available in the Dao interface and the
QueryBuilder classes don't provide the ability to handle all types of
queries.
Related
Is there any way I can create and drop tables similar to a 'RawQuery'?
I tried with a #RawQuery annotation (which it would be the perfect solution for me) but when I am compiling I get an error saying methods annotated with RawQuery can't return void.
I read only SELECT, UPDATE and DELETE statements are allowed when using #Query.
I would like to achieve the "creation or deletion of tables" by passing a tablename as a parameter, something like the following:
#Query("DROP TABLE :name")
void deleteTable (String name);
Any ideas on how to achieve this?
Thanks!
Official doc states that,
RawQuery serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into
objects.
RawQuery methods must return a non-void type. If you want to execute a raw query that does not return any value, use
RoomDatabase#query methods.
or use it like,
#RawQuery
int deleteTable (SupportSQLiteQuery query); //We can return int status like it used to return with database.delete()
//Usage
dao.deleteTable(
new SimpleSQLiteQuery("DROP TABLE tablename")
)
The ting is, wit Room, you don't have to "drop" tables, the tables re created based on your entity classes (annotated with #Entity).
As far as I know, you usually need to drop tables in case the columns change or there are some updates on the "structure", with Room there's no point in doing this unless you change the structure of your entity that can't be automatically handled by the migration. In this case, Room gives you the chance to do the migration by yourself. Check the documentation here: https://developer.android.com/training/data-storage/room/migrating-db-versions
But like the documentation states, be really careful with this.
in ORMLite document i can not find any document about this SQL command:
SELECT * FROM table WHERE state <> 1
i can get query where equal by this code:
List<ContactLists> contacts = G.CONTACTLISTSDAO.queryForEq("state","1");
how to change this code to NOT Equal
how to change this code to NOT Equal
The ORMLite DAO has a simple queryForEq(...) method. If you look at the code for queryForEq(...) you will see that it is just a convenience method for:
return queryBuilder().where().eq(fieldName, value).query();
This means that you can change the where to use ne(...) instead of eq(...):
return queryBuilder().where().ne(fieldName, value).query();
To do more complex queries you should RTFM about the QueryBuilder.
The QueryBuilder uses the where() method and Where class to define the WHERE portions of the query. There is eq for equals, gt for greater-than, lt for less-than, ne for not-equals, etc.. So, as #BrownKang points out, your query would be something like:
List<ContactLists> contacts =
G.CONTACTLISTSDAO.queryBuilder().where().ne("state", "1").query();
You can use "ne"
List<ContactLists> contacts =
G.CONTACTLISTSDAO.queryBuilder().where().ne("state", "1").query();
Use like this.
I have three tables that I have to present in one Android ListView. To get the data I use the SQL UNION operator to "merge" all three tables together, so that in my ViewBinder I can make each timeline item look distinct.
These items need to be sorted in chronological order. These three tables do not have a common base class.
Here is the SQL that I have in mind:
SELECT * FROM (
SELECT id, startTime as time, username, comment, "CustomerInteraction" FROM CustomerInteraction
UNION
SELECT id, date as time, "" as username, "" as comment, "Sale" FROM Sale
UNION
SELECT id, claimDate as time, username, comment, "TravelClaim" FROM TravelClaim)
ORDER BY time DESC LIMIT 100
How can I express the above query in ORMLite?
I know I can use Dao.executeRaw, but I don't want to populate my entire list in one go. I would much rather use the trick to get the underlying cursor from ORMLite, and then just pass that to my Adapter. (Lazy loading, makes initial display of long lists much faster.)
Is there a way I can do something like Dao.getPreparedQueryFromRaw(String statement) ? Or better yet QueryBuilder.union(QueryBuilder qb)?
You can get a Cursor by calling rawQuery on the SQLiteDatabase. I do something like this:
final SQLiteDatabase db = getHelper().getReadableDatabase();
Cursor cursor = db.rawQuery(MY_SQL_QUERY, null);
You don't need to do anything much more than that.
Is there a way I can do something like Dao.getPreparedQueryFromRaw(String statement) ? Or better yet QueryBuilder.union(QueryBuilder qb)?
The best way to do this with ORMLite is with one of the the queryRaw(...) methods. They return a GenericRawResults class which you can iterate across. The iterator gives you a number of different methods to help with moving around the list.
The problem is that the generic results are not of a certain type so I'm not sure if you can map it into the Android ListView. You can provide a RawRowMapper to queryRaw(...). You can get the mapper for a particular type by using the dao.getRawRowMapper() method.
Hope something here is helpful.
i want to create an inner join on three tables like this one for example:
SELECT C.Description, D.ItemDescription
FROM OrderDetailStatement AS D
INNER JOIN OrderHeaderStatement AS H
ON H.OrderHeaderStatementRefID = D.OrderHeaderStatementRefID
INNER JOIN customers AS C
ON H.CustomerRefID = C.CustomerRefID
WHERE (D.MixedValue > 1000)
but i'm a little bit confused, could you please provide me a walkthrough?
thanks in advance
ORMLite now supports simple JOIN statements. You can do something like the following:
// start the order header query
QueryBuilder<OrderHeader, Integer> orderHeaderQb = orderHeaderDao.queryBuilder();
QueryBuilder<Customer, Integer> customerQb = customerDao.queryBuilder();
// join with the order query
orderHeaderQb.join(customerQb);
// start the order statement query
QueryBuilder<OrderStatement, Integer> orderStatementQb =
orderStatementDao.queryBuilder();
orderStatementQb.where().gt("mixedvalue", 100);
// join with the order-header query
orderStatementQb.join(orderHeaderQb);
List<OrderStatement> orderStatementQb.query();
Notice, however, that you can only get entities from the query builder using this mechanism. If you want to get your two description fields from different objects then you would have to still use a raw-query.
There is support for "raw queries" including the Dao.queryRaw() method where you can use your own SQL. I suspect you've found them already. Here are the docs for raw queries.
I am trying to build a query in SQLite in Android. Query is very simple and looks like this:
SELECT
ifnull(object.icon_large, ifnull( object.image, item.IMG_DEF_COVER))
FROM
object , item
WHERE
object.id_item = 1 AND item.id_item = 1;
I've tried to use SQLiteDatabase.query(), but it can be used only for one table. Any suggestions how to combine queries or how to join them, using Android SQLiteDatabases query's syntax?
Thanks in advance!!!
#fox you can use rawquery() instead of query example:
SQLiteDatabase mydatabase;
mydatabase.rawquery("select * from a, b where a.id=b.someid", null);
or can use querybuilder's setTable() method to specify the joins example
builder.setTables(TABLEA+" LEFT JOIN "+TABLEB+" ON "+TABLEB+"."+KEY_ID+"="+TABLEA+"."+KEY_B_ID);
SELECT
ifnull(object.icon_large, ifnull(object.image, item.IMG_DEF_COVER))
FROM
object
JOIN
item
ON
object.id_item = item.id_item
WHERE
object.id_item = 1;
SELECT syntax