FMDB: NULL Values Are Retrieved as Empty Strings

╄→尐↘猪︶ㄣ 提交于 2019-12-24 12:12:16

问题


I'm retrieving a customer record with FMDB and Swift using the (simplified) function below. When the optional value in the title column is NULLthe title member of the returned customer object is an empty string rather than nil, which is misleading. Can this be re-written such that NULL values are retrieved as nil? -- Ideally without testing for empty strings and setting nil explicitly (also wrong if the value is in fact an empty string)?

func getCustomerById(id: NSUUID) -> Customer? {

    let db = FMDatabase(path: dbPath as String)
    if db.open() {
        let queryStatement = "SELECT * FROM Customers WHERE id = ?"
        let result = db.executeQuery(queryStatement, withArgumentsInArray: [id.UUIDString])

        while result.next() {
            var customer = Customer();
            customer.id = NSUUID(UUIDString: result.stringForColumn("customerId"))
            customer.firstName = result.stringForColumn("firstName")
            customer.lastName = result.stringForColumn("lastName")
            customer.title = result.stringForColumn("title")
            return customer
        }
    }
    else {
        println("Error: \(db.lastErrorMessage())")
    }
    return nil
}

回答1:


The NULL values are returned as nil:

db.executeUpdate("create table foo (bar text)", withArgumentsInArray: nil)
db.executeUpdate("insert into foo (bar) values (?)", withArgumentsInArray: ["baz"])
db.executeUpdate("insert into foo (bar) values (?)", withArgumentsInArray: [NSNull()])

if let rs = db.executeQuery("select * from foo", withArgumentsInArray: nil) {
    while rs.next() {
        if let string = rs.stringForColumn("bar") {
            println("bar = \(string)")
        } else {
            println("bar is null")
        }
    }
}

That outputs:

bar = baz
bar is null

You might want to double check how the values were inserted. Specifically, were empty values added using NSNull? Or perhaps open the database in an external tool and verify that the columns are really NULL like you expected.



来源:https://stackoverflow.com/questions/31788843/fmdb-null-values-are-retrieved-as-empty-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!