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.
Related
I know that I can do that by using data class but I want to achieve Like this
// This is My Query
#Query("SELECT loan_amount,adv_interest_amount FROM new_pledge_receive WHERE (new_pledge_receive.bill_date BETWEEN :fromDate AND :toDate)")
fun getPledgeReceiveAmount(fromDate: Long,toDate: Long):LiveData<List<Pair<Double,Double>>> ```
Pair class has 2 properties: first and second. Try to set the name of the selected columns to fit these names.
Something like: SELECT my_custom_field as first, my_another_field as second FROM...
As they mention on the official website
For SELECT queries, Room will infer the result contents from the method's return type and generate the code that will automatically convert the query result into the method's return type. For single result queries, the return type can be any java object. For queries that return multiple values, you can use List or Array. In addition to these, any query may return Cursor or any query result can be wrapped in a LiveData.
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.
i`m trying to search a Sqlite Database , with this condition : i want to find a string using an Exact Keyword. let me explain this to you .
i have 3 rows as follow :
catching cold
i have a cat
two cats was seen in your house yesterday
i want to search these rows with keyword "cat" and i expect this result :
i have a cat
i am using this SQL code so far :
Select * FROM MyTable WHERE Mycolumn Like '%cat%'
But Returning Result is All these 3 Rows:
catching cold
i have a cat
two cats was seen in your house yesterday
What can i do to get my expected result?
thank you in advance.
The % character in the argument of a LIKE clause matches any string, including the empty string. Unfortunately, SQLite doesn't have the REGEXP function built in (and Android's SQLite doesn't have it).
What you can do instead is use FTS (full text search). How to do so is described here: https://www.sqlite.org/fts3.html#section_1_2
Using your example, you would set it up like so:
create virtual table textsearch using fts4(content);
insert into textsearch (content) values ('catching cold'), ('i have a cat'), ('two cats was seen in your house yesterday')
Then you can do a simple text query with the MATCH operator:
select * from textsearch where content match 'cat';
If you try the above in a sqlite3 shell, you'll see it returns only 'i have a cat'. There's a lot more you can do with the match operator, explained on the page I linked above.
You can use a regular expression with a special pattern for word boundaries.
Select * FROM MyTable WHERE Mycolumn = 'cat'
Corrected my answer i think that should work.
I'm passing the below statement as a rawQuery in Android:
SELECT DISTINCT ltUsers._id,ltUsers.NAME,ltUsers.GLOBAL_ID, ltGroups.GROUP_NAME
FROM ltUsers
JOIN ltGroups ON (ltUsers.GROUP_ID = ltGroups.GLOBAL_ID)
WHERE ltgroups.GLOBAL_ID = ? " +
ORDER BY ltUsers.NAME ASC,ltgroups.GLOBAL_ID ASC;
With the rawQuery as follows:
Cursor c = db.rawQuery(sql,args)
It works just fine if I pass a value to the parameter, e.g.
String[] args = new String[]{"2"}
However, I also want to be able to show all rows, unlimited by the GLOBAL_ID in the WHERE clause. Testing on a dump of my SQLite database outside of Android - as well as in Android by just writing the parameter directly into the statement - shows the following clause to be a valid way to do this:
WHERE ltGroups.GLOBAL_ID = ltGroups.GLOBAL_ID
Yet when I pass the field reference ltGroups.GLOBAL_ID or [ltGroups].[GLOBAL_ID] as a parameter it fails to return any rows in the rawQuery. Any ideas on why this might be happening? Happy to produce any extra information.
Parameters always replace specific values, not anything else.
When you put the string "ltGroups.GLOBAL_ID" into the parameters array, it is interpreted as exactly that, a string.
(To show all records, just omit the WHERE clause.)
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.