mardi 18 août 2015

Can't access fts4 virtual table - SQLite DB Error: 1 "no such table:"

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
} 

Aucun commentaire:

Enregistrer un commentaire