I've got a database with several columns and a column name and I'm doing the following sqlite statement through OrmLite:
SELECT `name`,`id`,`ita_name`,`setCode`
FROM `MyTable`
WHERE (`name` LIKE ?)
ORDER BY `name` LIMIT 30
everything works fine but if the value of the name column is "Michael*,* Basketball player" and I submit "Michael Basketball player" the query return no results.
I've already tried to use %myQueryText% but with no success.
What can i do to return values from my column ignoring "," ?
SQL LIKE statement is more specific, than you think. The % wildcard matches zero or more characters at the position of the wildcard, so %myQueryText% matches anythingmyQueryText OR myQueryTextanything but not myQuery(comma)text. You need place wildcard at position of comma, see SQL LIKE wildcards.
So you probably want something like
... WHERE (`name` LIKE "Michael%Basketball player") ...
Edit/ Prepare your search pattern in Java, possibly like
String searchPattern = "Michael Basket player";
String preparedPattern = searchPattern.replace(' ', '%');
// outputs pattern "Michael%Basket%player"
Which could match even Michael the best Basketbal and other sports player and if you want to overcome such troubles, you can use REGEXP and prepare search pattern:
String searchPattern = "Michael Basket player";
String preparedPattern = searchPattern.replace(' ', '[,\s]+');
// outputs pattern "Michael[,\s]+Basket[,\s]+player"
and then in SQL query change WHERE statement to
WHERE `name` REGEXP "Michael[,\s]+Basket[,\s]+player"
the [,\s]+ matches either comma or white space and the occurance is one or multiple times, so it matches one space or space and comma or comma and more spaces...
Related
I have a book reader type app and I am using sqlite to store texts and provide search function which highlights search results returned by. My problem is that when for example I have the following text:
"Assuming N is a positive value, if no fragments can be found that
contain a phrase match corresponding to each matchable phrase, the
snippet function attempts to find two fragments of approximately N/2
tokens that between them contain at least one phrase match for each
matchable phrase matched by the current row. "
and I am searching for exact phrase "snippet function attempts", then I expect to get 1 search result, but I get 3 -> first is 'snippet', second is 'function' and third is 'attempts'.
My sqlite query is following:
'SELECT col1,col2, col3, offsets(index_table) FROM index_table WHERE col3 MATCH "snippet function attempts" '
How can I tell offsets() function to return offset for the whole phrase I am searching rather then individual parts of the phrase?
Try using :-
"SELECT col1,col2, col3, offsets(index_table) FROM index_table WHERE col3 MATCH 'snippet function attempts' "
i.e. single quotes around the phrase, as opposed to double quotes.
Enclosing the phrase in double quotes tells FTS that the values are a
list of phrases as per Phrase queries. A phrase query is a query that
retrieves all documents that contain a nominated set of terms or term
prefixes in a specified order with no intervening tokens. Phrase
queries are specified by enclosing a space separated sequence of terms
or term prefixes in double quotes ("). SQLite FTS3 and FTS4 Extensions
It continues to occur error... what is the problem?
every variables are String.
String insertSQL = "INSERT INTO " + DBHelper.getTableName() + " VALUES (\''"+entry.getKey()+"\'', \''"+images+"\'')";
Error message
INSERT INTO LABELING RESULT VALUES (''Sky'',
''["/storage/emulated/0/DCIM/CandyCam/IMG_20171009_164101723.jpg","/storage/emulated/0/DCIM/Pictail/IMG_20180305_000218777.jpg","/storage/emulated/0/DCIM/Pictail/IMG_20180401_235850170.jpg","/storage/emulated/0/DCIM/Pictail/IMG_20180518_194252232.jpg"]''))
My table has three column : ID(Integer), LABEL(TEXT), IMAGES(TEXT)
Issue 1 you have specified a table name with a space i.e. LABELING RESULT, if this is the table name then you would need to enclose it e.g. [LABELING RESULT].
Issue 2 you have double single quotes when you should only have 1.
Issue 3 you have an extra closing parenthesis.
I believe the following is what you want :-
INSERT INTO [LABELING RESULT] VALUES ('Sky', '["/storage/emulated/0/DCIM/CandyCam/IMG_20171009_164101723.jpg","/storage/emulated/0/DCIM/Pictail/IMG_20180305_000218777.jpg","/storage/emulated/0/DCIM/Pictail/IMG_20180401_235850170.jpg","/storage/emulated/0/DCIM/Pictail/IMG_20180518_194252232.jpg"]'))
Which would (not checked though) be :-
String insertSQL = "INSERT INTO [" + DBHelper.getTableName() + "] VALUES ('"+entry.getKey()+"', '"+images+"')";
That assumes that Sky goes into the LABEL column and the 2 comma separated image paths go into the IMAGES column.
Additional re enclosing (the [ ] ):-
If you want to use a keyword as a name, you need to quote it. There
are four ways of quoting keywords in SQLite:
'keyword' A keyword in single quotes is a string literal.
"keyword" A keyword in double-quotes is an identifier.
[keyword] A keyword enclosed in square brackets is an identifier. This is not
standard SQL. This quoting mechanism is used by MS Access and SQL
Server and is included in SQLite for compatibility.
keyword A keyword enclosed in grave accents (ASCII code 96) is an identifier.
This is not standard SQL. This quoting mechanism is used by MySQL and
is included in SQLite for compatibility.
SQL As Understood By SQLite - SQLite Keywords
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 || '%'
I am trying to search a String in column of my table .The values are stored in
column in this format
My Search String is similar as above. i.e: comma separated String
My query is to find out whether any value in search String is present is my column or not.
Things I have Tried:
using Like : Like does it.But it matches individual values i.e for 3 search string values I have to make 15 like condition(costly for large table)
SELECT * from A WHERE mycol LIKE 'myval,%' or mycol LIKE '%, myval,%' or mycol or LIKE '%, myval'
using instr function : instr matches the String but only the First Occurance.
e.g: select * from A where instr ('123' ,'12')
using In : This donot search within the String .But Matches the values individually.
I have tried Like function Like(X , Y) from docs Here
So my Question is there a way , instead of Individually searching my search string in the column using Like Operator or In function . I could search my values values in single query something like combination of In and Any(not supported in Sqlite) function does in other Db
i.e select * from A where Any('my search string') in ('my column value')
Any Answer or comment is HIGHLY Appreciated.Hoping for reply.
You have problems because your database is not properly normalized.
To have multiple search IDs for each MyTable row, create a second table that can have multiple rows, with one search ID in each row:
CREATE TABLE MyTable (
MyTableID INTEGER PRIMARY KEY,
[...]
);
CREATE TABLE MyTableSearchIDs (
MyTableID INTEGER REFERENCES MyTable(MyTableID),
SearchID INTEGER -- or TEXT or whatever
);
To search for a MyTable row, search its ID first:
SELECT *
FROM MyTable
WHERE MyTableID IN (SELECT MyTableID
FROM MyTableSearchIDs
WHERE SearchID = 'myvalue')
The same can be done with a join:
SELECT MyTable.*
FROM MyTable
JOIN MyTableSearchIDs USING (MyTableID)
WHERE MyTableSearchIDs.SearchID = 'myvalue'
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<?)