我想使用SQLite.swift查詢FTS表。 Previously I have done it in Android。什麼我試圖做的實質是this:如何使用IN條件訪問SQLite.swift中的FTS表
SELECT *
FROM t2
WHERE id IN (SELECT docid
FROM fts_table
WHERE col_text MATCH 'something')
從SQLite.swift documentation我看到IN
condition可以這樣寫this:
users.filter([1, 2, 3, 4, 5].contains(id))
// SELECT * FROM "users" WHERE ("id" IN (1, 2, 3, 4, 5))
和虛擬表可以查詢像this:
let wonderfulEmails = emails.match("wonder*")
// SELECT * FROM "emails" WHERE "emails" MATCH 'wonder*'
let replies = emails.filter(subject.match("Re:*"))
// SELECT * FROM "emails" WHERE "subject" MATCH 'Re:*'
但是,我不知道如何結合這些。我真的不想要execute arbitrary SQL(儘管這現在工作得益於this answer的幫助)。
更新
這是我最新的嘗試是不工作:
let tableText = Table("t2")
let id = Expression<Int64>("id")
let someColumn = Expression<String>("someColumn")
let tableFts = VirtualTable("fts_table")
let columnDocId = Expression<Int64>("docId")
let ftsQuery = tableFts
.select(columnDocId)
.filter(tableFts.match("something"))
let tableQuery = tableText
.filter((try db.prepare(ftsQuery))
.contains(id)) // <-- error
for row in try db.prepare(tableQuery) {
print(row[someColumn])
}
其中具有contains(id)
行引發錯誤:
Cannot convert value of type 'Expression' to expected argument type '@noescape (Row) throws -> Bool'
您是否有示例代碼片段和失敗?你想做什麼不起作用? – stephencelis
@stephencelis,我已添加更新。在這次最新的嘗試中,我嘗試將它分解爲兩個查詢,因爲我不知道有任何方法可以在單個查詢中執行。 – Suragch
我不相信你能夠在FTS表的查詢中使用表達式。如果您查看fts表的db模式,您會注意到type列是空的,而不是顯示預期的類型(如常規表中所述)。不確定爲什麼sqlite會像這樣工作,但我已經能夠工作的唯一一件事是循環遍歷返回的語句行。想知道你是否能夠進一步提高這一點。 –