SQLite query with multiple choice - android

Hi i'm will be wanna get rows than contains one of arguments of my string.
I have string as: "One,Two,Three,". I wanna get cursor with all data from table, that have element from this string ( For example, my find String is my string "rock,is,dead" and i wanna get rows that field contains: "rock".
Please help me, thanks

SELECT * FROM Your_Table WHERE 'rock,is,dead' LIKE '%' || String_Column || '%'
For more efficient lookups, you need to split up your search string so that the words can be checked individually, which allows the database to use index lookups (if you have an index):
SELECT * FROM Your_Table WHERE String_Column IN ('rock', 'is', 'dead')
(Please note that plain comparisons, unlike LIKE, are case sensitive, unless you use a different collation.)

Related

Sqlite query to find all rows where a particular column field value lies in input string

I got a scenario where I have to find all rows in Android SQLite db, where a particular column field value lies in the input string. For example,
Input string:
I am not done yet, I can’t call it a day I need to work more for couple of hours.
Now my table in SQLite(Android) has a column called Title
I need to find all rows whose Title column field values lies in above input string. Basically I want following rows
where the full Title string is like one of below
call it a day
am not done yet
I want to know if it is possible using a single query in sqlite Android or not. What I know is LIKE query work on opposite manner.
Given that your table contains a column title which has titles that you want to match in the given string, you can do concatenation with % to use LIKE
select *
from your_table
where 'your input string here' like '%' || title || '%'

sqlite android concatenation not works

I try to make query from table with search by first and last names columns.
When input simple name with 1 word (like "Tom" ) all fine. Query with 2 or more search keywords not works. F.e "Anny Lee". Is there possible way to search by concatenated contacts_table.first_name and contacts_table.last_name value in DB table? The part of query below:
AND ( (contacts_table.first_name || contacts_table.last_name LIKE '%KEYWORD%') OR (....))
Cant implement OR statement in right way.

SQLite Fts select query

I am making a dictionary of over 20,000 words in it. So, to make it work faster when search data, i am using fts3 table to do it.
my select query:
Cursor c=db.rawQuery("Select * from data where Word MATCH '"+word+"*'", null);
Using this query, it will show all the word that contain 'word' , but what i want is to get only the word that contain the beginning of the searching word.
Mean that i want it work like this query:
Cursor c=db.rawQuery("Select * from data where Word like '"+word+"%'", null);
Ex: I have : apple, app, and, book, bad, cat, car.
when I type 'a': i want it to show only: apple, app, and
What can i solve with this?
table(_id primary key not null autoincrement, word text)
FTS table does not use the above attributes. It ignores data type. It does not auto increment columns other than the hidden rowid column. "_id" will not act as a primary key here. Please verify that you are implementing an FTS table
https://www.sqlite.org/fts3.html
a datatype name may be optionally specified for each column. This is
pure syntactic sugar, the supplied typenames are not used by FTS or
the SQLite core for any purpose. The same applies to any constraints
specified along with an FTS column name - they are parsed but not used
or recorded by the system in any way.
As for your original question, match "abc*" already searches from the beginning of the word. For instance match "man*" will not match "woman".
FTS supports searching for the beginning of a string with ^:
SELECT * FROM FtsTable WHERE Word MATCH '^word*'
However, the full-text search index is designed to find words inside larger texts.
If your Word column contains only a single word, your query is more efficient if you use LIKE 'a%' and rely on a normal index.
To allow an index to be used with LIKE, the table column must have TEXT affinity, and the index must be declared as COLLATE NOCASE (because LIKE is not case sensitive):
CREATE TABLE data (
...
Word TEXT,
...
);
CREATE INDEX data_Word_index ON data(Word COLLATE NOCASE);
If you were to use GLOB instead, the index would have to be case sensitive (the default).
You can use EXPLAIN QUERY PLAN to check whether the query uses the index:
sqlite> EXPLAIN QUERY PLAN SELECT * FROM data WHERE Word LIKE 'a%';
0|0|0|SEARCH TABLE data USING INDEX data_Word_index (Word>? AND Word<?)

How to query similar records in SQLite database from Android?

Let's say an SQLite database column contains the following value:
U Walther-Schreiber-Platz
Using a query I'd like to find this record, or any similar records, if the user searches for the following strings:
walther schreiber
u walther
walther-schreiber platz
[Any other similar strings]
However I cannot figure out any query which would do that. Especially if the user does not enter the - character.
Example:
select * from myTable where value like '%walther schreiber%'; would not return the result because of the missing -.
Thanks,
Robert
So, as I said in my comment, I think you can have a query along the lines of:
select * from myTable where LOWER(value) like <SearchValue>
I'm assuming you're collecting the <SearchValue> from the user programmatically, so would be able to do the following: <SearchValue> would need to be: The user's search string, appropriately cleansed to avoid SQL injection attacks, converted to lower case, with all of the spaces converted to '%', so that they match zero or more characters...
So you would have (for example):
select * from myTable where LOWER(value) like '%walther%schreiber%'
select * from myTable where LOWER(value) like '%walther-schreiber%platz%'
etc... however, this does assume that the word order is the same, which in your examples it is...

Query to sort a field such that strings comes first and then numbers

I've a column in which contains numbers or strings. The type of the column is varchar.
Usually when we sort it using the string field, then all the numbers come first and then strings start. But I want all the strings first and then numbers.
TIA !
You'll have to write it in two separate queries. One for selecting numbers, the other for strings. Preferably I would create a second column (one for numbers, one for strings), making it easier and faster to have those two queries run.
This worked for me...
Select * from Table order by stringfield+0;
edit: http://www.sqlite.org/datatypes.html (Point 4.0)
UPDATE: Try this....
select * from Table where LENGTH(trim(stringfield,"0123456789 ") )=0 union select * from table order by stringfield;
How about the following (two queries as suggested above):
select * from Table where LENGTH(trim(stringfield,"0123456789 ")) > 0; select * from table where LENGTH(trim(stringfield,"0123456789 ")) = 0;
The first select should return only values that are not numeric, whilst the second should return only values that are numeric.
For a table that contains a mixture of numeric and string data, this outputs the strings first, then the numbers.
Have you considered creating a custom collation-function? I have never used this myself, but it sounds like exactly what you need.

Categories

Resources