lundi 29 février 2016

XCode Project Using FMDB, SQLite Table Not Found

I'm new to XCode and have been trying to create what I'd hoped would be a simple app to scan a barcode and check the value it grabs against a database to pull up the info associated with that code.

I'm trying to use FMDB with Swift to do this, but I can't seem to be able to get the database to copy over to the app correctly and am now getting the following error:

DB Error: 1 "no such table: building"

My code attempting to open and query the database for the barcode description is:

import UIKit

let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)

let fileURL = documents.URLByAppendingPathComponent("barcodeDatabase.db")

let database = FMDatabase(path: fileURL.path)

class ViewController: UIViewController {

    @IBOutlet weak var scannedCode: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func codeSet(sender: UIButton) {
        if !database.open() {
            print("Unable to open database")
            return
        }
        do {
            let temp = try database.executeQuery("select * from building where location_id = ?", values: [myVars.codeData])
            while temp.next() {
                scannedCode.text = temp.stringForColumn("location_desc") + "\r\n" + myVars.codeData
            }
        } catch let error as NSError {
                print("failed: \(error.localizedDescription)")
        }

        database.close()
    }

}

I have the database file "barcodeDatabase.db" added to the project, and it's included in the project target membership and the copy bundle resources area.

I was able to make it work in the simulator by commenting out

let fileURL = documents.URLByAppendingPathComponent("barcodeDatabase.db")

and changing fileURL.path in the next line to the path in my mac's documents, but on the actual iPad it said it was unable to open the database.

Aucun commentaire:

Enregistrer un commentaire