How to write contains query in SQLite fts3 fulltext search - android

I want to make fulltext search in my index table which is sqlite fts3.
For example;
the data set is { "David Luiz", "David Villa", "Diego Costa", "Diego Ribas", "Diego Milito","Gabriel Milito", }
When I type "vid i" I want to get {"David Luiz", "David Villa"}
In documentation of SQLite I found this
http://www.sqlite.org/fts3.html#section_3
but it contains just startswith query.
my query is:
SELECT *FROM Table WHERE Table MATCH "*vid* *i*"
I dont know it is possible or not. If it is possible to make search in sqlite fts3, any help will be appreciated

The FTS index is optimized for word searches, and supports word prefix searches.
There is no index that can help with searches inside words.
You have to use LIKE '%vid%' (which scans the entire table).

Change your query from
SELECT * FROM Table WHERE Table MATCH "*vid* *i*"
To
SELECT * FROM SOME_TABLE WHERE some_column LIKE '%vid%'

Related

Sqlite query to get list of next available characters after the matching search keyword

How to query sqlite database to get list of next available characters after the search keyword in a string field.
for example, if the search keyword is ’and’
and let the strings be a list of names like :
Andy Xyz
Andrew Xyz
Xyz Andon
Xyz Miranda
then i should get the characters [y,r,o,a].
I tried the query with substr(names,1,1), but substr needs to specify the start index which will be different in each strings. Is it possible to fetch these characters using sqlite query?
You need substr() and instr():
select
substr(names, instr(lower(names), lower('and')) + length('and'), 1) nextchar
from tablename
where
names like '%' || 'and' || '_%'
See the demo
You can replace 'and' with any string you wish in the above query.
You could utilise the instr function to ascertain the start of the substr e.g. :-
SELECT *, substr(names,instr(lower(names),'and') + length('and'),1) AS onechar
FROM mytable WHERE names LIKE ('%and%');
A working example :-
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (names TEXT);
INSERT INTO mytable VALUES('Andy Xyz'),('Andrew Xyz'),
('Xyz Andon'),('Xyz Miranda');
SELECT *, substr(names,instr(lower(names),'and') + length('and'),1) AS onechar FROM mytable WHERE names LIKE ('%and%');
This results in :-
as per your expected results.

Android Sqlite FTS3 how to select words that starts with?

For example, if i have these records
word
AAA
AAB
AAC
BAA AA
With a normal table i would use sql like
select * from table where word like 'AA%'order by H collate nocase asc
How do i select with FTS3 table instead?
Also i would like to know if FTS3 will still have better performance than normal table with this kind of query?
How do i select with FTS3 table instead?
Quoting the documentation:
An FTS table may be queried for all documents that contain a specified term (the simple case described above), or for all documents that contain a term with a specified prefix. As we have seen, the query expression for a specific term is simply the term itself. The query expression used to search for a term prefix is the prefix itself with a '*' character appended to it.
The documentation also gives a sample:
-- Query for all documents containing a term with the prefix "lin". This will match
-- all documents that contain "linux", but also those that contain terms "linear",
--"linker", "linguistic" and so on.
SELECT * FROM docs WHERE docs MATCH 'lin*';

speedup my search query in android sqlite?

I create a program that search between 20000 record.
I implement this search in
edit.addTextChangedListener(new TextWatcher() {...}
When user enter a character search query execute.but very slow return answer.
this is my record table :
id, prodname, proddet, prodid, refid
and this is my search query:
SELECT DISTINCT prodname , prodid FROM tblProd where ((prodname like '%"+name+"%') or (proddet like '%"+name+"%') and (prodname IS NOT NULL)) LIMIT 20;
Also my table is fts3 and use this query for search :
SELECT DISTINCT prodname , prodid FROM tblProd where (tblProd MATCH '"+name+"*') LIMIT 20
but speed is slow like other table.
any one can help me how to speed up this search without delay.
and sorry for my bad english.
In FTS tables, the only efficient search is with MATCH.
If you don't have duplicate product IDs, remove the DISTINCT (which requires reading all rows before applying the LIMIT).

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<?)

android _Displaying a record from a sqlite database

I am planning to build an app that lets the user select a record from a particular database such as:
name favorite_color favorite_team
sue red Dal
mike blue Mia
sam purple Bal
My problem is that most of the tutorials that I have come across only demonstrates examples using a table with one column. What if my pre-populated database more then on column? What if it had 100 columns? Does anyone know how this is done?????
Here's an example:
Step1:Declare SQLiteDatabase,Declare Databasehelper
Step2:Declare string or number which is to be used as key or get it as an intent(in this example 'rowid'
Step 3: In the OnCreate method add lines of code similar to following:
String query="select * from my_table_name where _id="+rowid;
Cursor myCursor = database.rawQuery(query,null);
myCursor.moveToFirst();
//This line implies i am getting data from column four of selected row
String x=myCursor.getString(4);
//This line implies i am getting data from column two of selected row
String y=myCursor.getString(4);
myCursor.close();
Note:
a)Don't forget that your database size must not exceed 1.2mb
b)Also include a column with name _id which auto-increments in each table that you are using,you may use sqlite browser to do so
c)also create the following table in your database :
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
Now insert a single row with the text 'en_US' in the "android_metadata" table
INSERT INTO "android_metadata" VALUES ('en_US')
read this you will learn every thing you need to know. One other way to access db information is using an ORM like ormlite. I'm using it in various apps that i've developed, and it's simple to use.

Categories

Resources