简体   繁体   中英

How to search the database for a field which is a substring of the query by using sqlite

Problem description

I want to search for the query = Angela in a database from a table called Variations. The problem is that the database does not Angela . It contains Angel . As you can see the a is missing.


Searching procedure

The table that I want to query is the following:

"CREATE TABLE IF NOT EXISTS VARIATIONS 
    (ID        INTEGER PRIMARY KEY NOT NULL, 
     ID_ENTITE INTEGER, 
     NAME      TEXT, 
     TYPE      TEXT, 
     LANGUAGE  TEXT);"

To search for the query I am using fts4 because it is faster than LIKE% especially if I have a big database with more than 10 millions rows. I cannot also use the equality since i am looking for substrings.

  1. I create a virtual table create virtual table variation_virtual using fts4(ID, ID_ENTITE, NAME, TYPE, LANGUAGE);
  2. Filled the virtual table with VARIATIONS insert into variation_virtual select * from VARIATIONS;
  3. The selection query is represented as follow:

     SELECT ID_ENTITE, NAME FROM variation_virtual WHERE NAME MATCH "Angela"; 

Question

What am I missing in the query. What I am doing is the opposite of when we want to check if a query is a subtring of a string in a table.

You can't use fts4 for this. From the documentation :

SELECT count(*) FROM enrondata1 WHERE content MATCH 'linux';  /* 0.03 seconds */
SELECT count(*) FROM enrondata2 WHERE content LIKE '%linux%'; /* 22.5 seconds */

Of course, the two queries above are not entirely equivalent. For example the LIKE query matches rows that contain terms such as "linuxophobe" or "EnterpriseLinux" (as it happens, the Enron E-Mail Dataset does not actually contain any such terms), whereas the MATCH query on the FTS3 table selects only those rows that contain "linux" as a discrete token. Both searches are case-insensitive.

So your query will only match strings that have 'Angela' as a word (at least that is how I interpret 'discrete token').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM