Search code examples
iosswift3fmdb

FMDB stringforcolumn with nil


I apologize if this question was asked before (couldn't find an exact solution) but I have a column on my database table that is of type varchar and is nullable.

When I query to get back the column:

 let rs: FMResultSet = db.executeQuery("SELECT InternalNote from MyTable where id = ?", withArgumentsIn: [id])

        while(rs.next())
        {
           let testValue = rs.string(forColumn: "InternalNote") 
            ..code continues..
        }
..

it causes a crash on the rs.string above because the field is null..

I figured I'll have to just change it to be non-null and default of "", but is there a way I can keep it null?

Thanks!


Solution

  • What if you change your code to:

    if let value = rs.string(forColumn: "internalNote") {
        let testValue = value 
        ... code continues ...
    }