我有一个用户正在破坏我的代码,我认为这是因为他的名字里有一个撇号。我已经尝试使用双撇号和反斜杠进行转义,但我一直收到“fatal error: unexpectedly found nil while unwrapping an Optional value”错误提示。
let querySQL = "SELECT name from member_data ORDER BY RANDOM() LIMIT 1"
rightAnswer = memberDatabase!.executeQuery(querySQL, withArgumentsInArray: nil)
rightAnswer!.next()
correctName = rightAnswer!.stringForColumn("name")!
println("Correct answer is \(correctName)")
//prints as Optional("Sam O\'Neal")
let sanitizedName = correctName?.stringByReplacingOccurrencesOfString("'", withString: "\'")
//tried many combinations of this
let wrongAnswerSQLQuery = "SELECT name from member_data where name is not '\(sanitizedName)' LIMIT 3"
let wrongAnswersResultSet:FMResultSet = memberDatabase!.executeQuery(wrongAnswerSQLQuery, withArgumentsInArray: nil)
?
占位符的方法是最好的。 - Rob