我试图将FMDB SQLite查询的结果附加到Swift数组。我在XCode中遇到的错误是选项类型的值'字符串?'没有打开。'
在while循环中交换下面的行,FMDB结果可以打印到控制台OK,所以没有问题。
println(results_lab_test?.stringForColumn("lab_test"))
XCode新手,所以要善待......
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tests_label: UILabel!
var databasePath = NSString()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var arrayData:[String] = []
let filemgr = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir = dirPaths[0] as! String
databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")
let myDatabase = FMDatabase(path: databasePath as String)
if myDatabase.open(){
let query_lab_test = "SELECT lab_test FROM lab_test"
let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)
while results_lab_test?.next() == true {
if let resultString = results_lab_test?.stringForColumn("lab_test"){
arrayData.append(resultString)
var multiLineString = join("\n", arrayData)
tests_label.text = multiLineString
tests_label.numberOfLines = 0
tests_label.lineBreakMode = NSLineBreakMode.ByWordWrapping
tests_label.sizeToFit()
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
工作代码归功于@skypecakes:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tests_label: UILabel!
var databasePath = NSString()
override func viewDidLoad() {
super.viewDidLoad()
let filemgr = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir = dirPaths[0] as! String
databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")
let myDatabase = FMDatabase(path: databasePath as String)
if myDatabase.open(){
var arrayData:[String] = []
let query_lab_test = "SELECT lab_test FROM lab_test"
let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)
while results_lab_test?.next() == true {
if let resultString = results_lab_test?.stringForColumn("lab_test"){
arrayData.append(resultString)
}
}
println(arrayData)
myDatabase.close()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:3)
results_lab_test?.stringForColumn("lab_test")
返回一个String?,这是一个可选的。您的数组被定义为“String”项的数组,因此您不能放置“String?”在里面。
试试这个:
if let resultString = results_lab_test?.stringForColumn("lab_test")
arrayData.append(resultString)
请注意,一般情况下,最好使用“if let”展开所有选项,而不是假设它们已填充。所以无论你有问号(例如results_lab_test?.stringForColumn),都可以使用“if let”。
如果您正在使用带有Swift 2.0的XCode 7,这对于“guard let”语句来说是一个很好的例子,如果可选的返回null,它提供了一个方便的语法来中止代码:
guard let queryResults = results_lab_test else return
while queryResults.next() == true {
if let resultString = queryResults.stringForColumn("lab_test")
arrayData.append(resultString)
}
此测试适用于我(表格有2行,打印数组打印2行),以防它帮助您:
import UIKit
import FMDB
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
if let myDatabase = makeSqlDB()
{
var arrayData:[String] = []
let query_lab_test = "SELECT lab_test FROM lab_test"
let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)
while results_lab_test?.next() == true
{
if let resultString = results_lab_test?.stringForColumn("lab_test")
{
arrayData.append(resultString)
}
}
println(arrayData)
myDatabase.close()
}
}
private func makeSqlDB()->FMDatabase?
{
let database = FMDatabase(path: String())
if !database.open() {
println("Unable to open database")
return nil
}
if !database.executeUpdate("create table lab_test(lab_test text)", withArgumentsInArray: nil) {
println("create table failed: \(database.lastErrorMessage())")
}
if !database.executeUpdate("insert into lab_test (lab_test) values (?)", withArgumentsInArray: ["test1"]) {
println("insert 1 table failed: \(database.lastErrorMessage())")
}
if !database.executeUpdate("insert into lab_test (lab_test) values (?)", withArgumentsInArray: ["test2"]) {
println("insert 2 table failed: \(database.lastErrorMessage())")
}
return database
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}