"unexpectedly found nil while unwrapping an Optional value" 在 SQLite 数据库中返回一行时出错
"unexpectedly found nil while unwrapping an Optional value" error when returning a row in SQLite DB
我已经将 FMDB Sqlite Wrapper 集成到我的 iOS 应用程序中,并且我编写了几个数据库函数,到目前为止它们都运行良好。
我对一个简单的方法使用了相同的方法,该方法应该 return 结果集项目中的单个元素,但出现上述错误。与其他方法的逻辑没有什么不同,所以我不知道为什么会发生错误以及为什么它会在 stringForColumn 的两行而不是 intForColumn 的行中发生(正如调试器告诉我的那样)。
如有任何帮助或提示,我们将不胜感激!
func fetchExercise(exerciseId: Int) -> Exercise {
sharedInstance.database!.open()
let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("Select * from Exercises where ExerciseId = ?", withArgumentsInArray: [String(exerciseId)])
let fetchedExercise: Exercise = Exercise()
if (resultSet != nil) {
fetchedExercise.exerciseId = Int(resultSet.intForColumn("ExerciseId"))
fetchedExercise.exerciseCategory = resultSet.stringForColumn("ExerciseCategory")
fetchedExercise.exerciseTitle = resultSet.stringForColumn("ExerciseTitle")
}
sharedInstance.database!.close()
return fetchedExercise
}
resultSet
如下:
您必须调用 resultsSet.next()
才能检索结果。例如:
func fetchExercise(exerciseId: Int) -> Exercise {
sharedInstance.database!.open()
defer { sharedInstance.database!.close() }
let resultSet = try! sharedInstance.database!.executeQuery("Select * from Exercises where ExerciseId = ?", values: [exerciseId])
let fetchedExercise = Exercise()
if resultSet.next() {
fetchedExercise.exerciseId = resultSet.longForColumn("ExerciseId")
fetchedExercise.exerciseCategory = resultSet.stringForColumn("ExerciseCategory")
fetchedExercise.exerciseTitle = resultSet.stringForColumn("ExerciseTitle")
}
return fetchedExercise
}
顺便说一句,我不确定您为什么要将 exerciseId
转换为查询中的字符串。我个人只是传递 Int
值,如上所示。
我已经将 FMDB Sqlite Wrapper 集成到我的 iOS 应用程序中,并且我编写了几个数据库函数,到目前为止它们都运行良好。
我对一个简单的方法使用了相同的方法,该方法应该 return 结果集项目中的单个元素,但出现上述错误。与其他方法的逻辑没有什么不同,所以我不知道为什么会发生错误以及为什么它会在 stringForColumn 的两行而不是 intForColumn 的行中发生(正如调试器告诉我的那样)。
如有任何帮助或提示,我们将不胜感激!
func fetchExercise(exerciseId: Int) -> Exercise {
sharedInstance.database!.open()
let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("Select * from Exercises where ExerciseId = ?", withArgumentsInArray: [String(exerciseId)])
let fetchedExercise: Exercise = Exercise()
if (resultSet != nil) {
fetchedExercise.exerciseId = Int(resultSet.intForColumn("ExerciseId"))
fetchedExercise.exerciseCategory = resultSet.stringForColumn("ExerciseCategory")
fetchedExercise.exerciseTitle = resultSet.stringForColumn("ExerciseTitle")
}
sharedInstance.database!.close()
return fetchedExercise
}
resultSet
如下:
您必须调用 resultsSet.next()
才能检索结果。例如:
func fetchExercise(exerciseId: Int) -> Exercise {
sharedInstance.database!.open()
defer { sharedInstance.database!.close() }
let resultSet = try! sharedInstance.database!.executeQuery("Select * from Exercises where ExerciseId = ?", values: [exerciseId])
let fetchedExercise = Exercise()
if resultSet.next() {
fetchedExercise.exerciseId = resultSet.longForColumn("ExerciseId")
fetchedExercise.exerciseCategory = resultSet.stringForColumn("ExerciseCategory")
fetchedExercise.exerciseTitle = resultSet.stringForColumn("ExerciseTitle")
}
return fetchedExercise
}
顺便说一句,我不确定您为什么要将 exerciseId
转换为查询中的字符串。我个人只是传递 Int
值,如上所示。