Reputation: 31
I'm quite new to programming so sorry if it is a silly question. I want to make a dictionary app on iOS and I want to use fts4 table for that. I use SQLite and FMDB. So, I create a virtual table, but when I try to find anything in it I get an exception it doesn't exist. What's wrong? I tried to search right after creating a virtual table without writing two distinct functions but still got an exception.
Here's the code:
let db : FMDatabase
let resourcePath = NSBundle.mainBundle().resourceURL!.absoluteString
let dbPath = resourcePath?.stringByAppendingPathComponent("dictionary.db")
db = FMDatabase(path: dbPath)
var res : FMResultSet?
if (createVirtualDB(db, "vdict") != nil) {}
else { println("Error while opening db")}
res = findInVirtualDB("test", db, "vdict")
I create virtual table with this code:
func createVirtualDB (db : FMDatabase, name : String) -> Int?
{
if db.open()
{
let querySQL = "CREATE VIRTUAL TABLE \(name) USING fts4(content=\"DICT2\", level, latin, russian)"
db.executeQuery(querySQL, withArgumentsInArray: nil)
}
else {return nil}
return 1
}
The search function looks like this:
func findInVirtualDB(searchedString: String, db : FMDatabase, name : String) -> FMResultSet?
{
let res : FMResultSet?
if db.open()
{
let querySQL = "SELECT level, latin, russian FROM \(name) WHERE latin MATCH '\(searchedString)'"
res = db.executeQuery(querySQL, withArgumentsInArray: nil)
}
else {return nil}
return res
}
Upvotes: 2
Views: 586
Reputation: 31
I found out the problem. The thing is that executeQuery() function only works with SELECT statements. For CREATE statement function executeStatements() should be used.
So I changed the code of createVirtualDB() function from:
let querySQL = "CREATE VIRTUAL TABLE \(name) USING fts4(content=\"DICT2\", level, latin, russian)"
db.executeQuery(querySQL, withArgumentsInArray: nil)
to this:
let querySQL = "CREATE VIRTUAL TABLE IF NOT EXISTS \(name) USING fts4(content=\"DICT2\", level, latin, russian)"
db.executeStatements(querySQL)
That worked fine.
Upvotes: 1