Skip to content Skip to sidebar Skip to footer

Append Fmdb Sqlite Results To Swift Array

I'm trying to append results from a FMDB SQLite query to a Swift array. The error I'm getting in XCode is 'value of option type 'String?' not unwrapped.' Swapping out the line b

Solution 1:

It seems like results_lab_test?.stringForColumn("lab_test") returns a String?, which is an optional. Your array is defined as an array of "String" items, so you can't put a "String?" inside it. Try this:

iflet resultString = results_lab_test?.stringForColumn("lab_test")
    arrayData.append(resultString)

Note that in general it is good practice to unwrap all of your optionals with "if let" instead of assuming that they are populated. So everywhere you have a question mark (e.g. results_lab_test?.stringForColumn), you can use "if let".

If you're using XCode 7 with Swift 2.0, this would be a good case for the "guard let" statement, which provides a convenient syntax to abort your code if an optional returns null:

guardlet queryResults = results_lab_test elsereturnwhile queryResults.next() ==true {
iflet resultString = queryResults.stringForColumn("lab_test")
        arrayData.append(resultString)
}

This test worked for me (table has 2 rows, and printing the array prints 2 rows), in case it helps you:

importUIKitimportFMDBclassViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        iflet myDatabase = makeSqlDB()
        {
            vararrayData:[String] = []
            let query_lab_test = "SELECT lab_test FROM lab_test"letresults_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)

            while results_lab_test?.next() == true
            {
                iflet resultString = results_lab_test?.stringForColumn("lab_test")
                {
                    arrayData.append(resultString)
                }
            }

            println(arrayData)
            myDatabase.close()
        }
    }


    private func makeSqlDB()->FMDatabase?
    {
        let database = FMDatabase(path: String())

        if !database.open() {
            println("Unable to open database")
            return nil
        }

        if !database.executeUpdate("create table lab_test(lab_test text)", withArgumentsInArray: nil) {
            println("create table failed: \(database.lastErrorMessage())")
        }

        if !database.executeUpdate("insert into lab_test (lab_test) values (?)", withArgumentsInArray: ["test1"]) {
            println("insert 1 table failed: \(database.lastErrorMessage())")
        }

        if !database.executeUpdate("insert into lab_test (lab_test) values (?)", withArgumentsInArray: ["test2"]) {
            println("insert 2 table failed: \(database.lastErrorMessage())")
        }

        return database
    }


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

Post a Comment for "Append Fmdb Sqlite Results To Swift Array"