2013-02-02 27 views
4

我已經下載了最新的SQLite 3.7.15.2外殼的(Win32),並試圖執行的FTS的例子只有一個,因爲它是在http://sqlite.org/fts3.html#section_3SQLite的FTS例如不工作

-- Virtual table declaration 
CREATE VIRTUAL TABLE docs USING fts3(); 

-- Virtual table data 
INSERT INTO docs(docid, content) VALUES(1, 'a database is a software system'); 
INSERT INTO docs(docid, content) VALUES(2, 'sqlite is a software system'); 
INSERT INTO docs(docid, content) VALUES(3, 'sqlite is a database'); 

-- Return the set of documents that contain the term "sqlite", and the 
-- term "database". This query will return the document with docid 3 only. 
SELECT * FROM docs WHERE docs MATCH 'sqlite AND database'; 

但儘管如此寫最後評論的SELECT導致空集。這是SQLite中的錯誤還是過時的文檔? (和那是什麼正確的語法?)。

什麼是對我來說最重要的是,查詢

SELECT * FROM docs WHERE docs MATCH '(database OR sqlite) NEAR/5 system'; 

無法正常工作或與該類型我需要在我的應用程序的查詢。有沒有其他的方式來寫它,所以它會工作?

+0

你,不管怎樣,平均' '源碼' 和「database''?編輯:忍受着我。我從來沒有在我的生活中使用SQLite :) –

+0

這是從SQLite文檔精確複製粘貼。我只想讓它按照 –

回答

3

從文檔中的示例使用了enhanced query syntax。 檢查PRAGMA compile_options;包括ENABLE_FTS3_PARENTHESIS

那你NEAR查詢不工作,不與編譯選項的問題:

> SELECT * FROM docs WHERE docs MATCH '(database OR sqlite) NEAR/5 system'; 
Error: malformed MATCH expression: [(database OR sqlite) NEAR/5 system] 

的問題是,根據該文件,NEAR不只是基本的搜索表達式工作:

通過在兩個短語,詞語或前綴查詢之間放入關鍵字「NEAR」來指定NEAR查詢。

所以,你必須相應地重寫你的檢索表達式:

> SELECT * FROM docs WHERE docs MATCH '(database NEAR/5 system) OR (sqlite NEAR/5 system)'; 
a database is a software system 
sqlite is a software system 
3

我不知道這是否是文檔,或者如果它是使用SQLite一個bug,但這裏有一些選擇:

AND查詢

不起作用:

select * from docs where docs match 'sqlite AND database'; 

工程(利用隱含AND):

select * from docs where docs match 'sqlite database'; 

OR似乎工作:

OR + NEAR查詢:

不起作用:

SELECT * FROM docs WHERE docs MATCH '(database OR sqlite) NEAR/5 system'; 

作品:

SELECT * FROM docs WHERE docs MATCH 'database NEAR/5 system' 
UNION 
SELECT * FROM docs WHERE docs MATCH 'sqlite NEAR/5 system' 

編輯:對於評論中提到的形式(word11 OR word12 OR word13) NEAR/2 (word21 OR word22 OR word23) NEAR/2 (word31 OR word32 OR word33。這是我能做的最好的是用UNION把所有的組合在一起:

SELECT * FROM docs WHERE docs MATCH 'word11 NEAR/2 word21 NEAR/2 word31' 
UNION 
SELECT * FROM docs WHERE docs MATCH 'word11 NEAR/2 word22 NEAR/2 word32' 
UNION 
SELECT * FROM docs WHERE docs MATCH 'word11 NEAR/2 word23 NEAR/2 word33' 
UNION 
SELECT * FROM docs WHERE docs MATCH 'word12 NEAR/2 word21 NEAR/2 word31' 
... 

以上的課程產生大量的SQL。如果你的話是,只有在不同的結局類似,你可以使用通配符:

SELECT * FROM docs WHERE docs MATCH 'word1* NEAR/2 word2* NEAR/2 word3*'; 
+0

的描述工作。不幸的是UNION不適合我,因爲我的查詢將是形式(word11或word12或word13)NEAR/2(word21或word22或word23)NEAR/2(word31 OR word32 OR word33)... –

+0

@StanLagun - 這些可能是可能的,但可能它們有點太複雜。我會看看,看看我能不能拿出任何東西... –

+0

@StanLagun - 我的迴應上面'編輯:'後。這些解決方案可能會或可能不會是您正在尋找的。 –