1
我有一個用戶打破了我的其他工作代碼,並且我認爲這是因爲他的名字有一個撇號。我嘗試使用雙撇號和反斜槓轉義它,但我一直得到fatal error: unexpectedly found nil while unwrapping an Optional value
。在Swift中,使用帶有撇號的SQL查詢
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)
嗯,完美!對於它的價值,我嘗試了將撇號加倍並且不成功(在其他人嘗試的情況下)。第三種解決方案顯然是最好的 - 謝謝! – thumbtackthief
是的,如果你走這條路線,你必須做兩個選項的加倍和解開。但正如你所說,使用'?'佔位符的方法是最好的。 – Rob