mardi 22 septembre 2015

Can not find tables in .sqlite file with FMDB by swift.

After i updated Xcode7 my app can connect the database normally but cannot find any table in the app

This is my code.

func openDB(){
    let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
    let path = NSURL(fileURLWithPath: documentsFolder).URLByAppendingPathComponent("baseDeck.sqlite")

    print(path)

    let stringPath = "\(path)" as String
    print(stringPath)

    let fileManager = NSFileManager()
    if (!fileManager.fileExistsAtPath(stringPath)){
        let dbFilePath = NSBundle.mainBundle().pathForResource("baseDeck", ofType: "sqlite")!
        do {
            try fileManager.copyItemAtPath(dbFilePath, toPath: stringPath)
        } catch _ {
        }
    }

    database = FMDatabase(path: stringPath)

    if !database.open() {
        print("Unable to open database")
        return
    }else{
        print("database is opened")
    }
} 

It says "database is opened". And then when i try to query some data in the table by this code

database.executeQuery("SELECT * FROM baseDeckTable ORDER BY id ASC", withArgumentsInArray: nil)

I got

"DB Error: 1 "no such table: baseDeckTable" 

But my database have that table for sure like an image below.

enter image description here

And i also sure that i included it in my Copy Bundle Resources like this

enter image description here

How can i fix it?

Thanks!

Aucun commentaire:

Enregistrer un commentaire