我需要使用for循环从数据库获取值以显示多个变量 我该怎么办?请帮助我。
我的变量:
var printerName1: String = ""
var printerName2: String = ""
var printerName3: String = ""
var printerName4: String = ""
var printerName5: String = ""
我的功能:
func load(){
var selectPrinter = [printerName1,printerName2,printerName3
,printerName4,printerName5]
let db = SQLiteDB.shared
db.openDB()
let data = db.query(sql: "select * from tbPrinter")
for index in 0..<data.count {
let row = data[index]
if let printerName = row["name"]{
selectPrinter[index] = printerName as? String //Cannot to set value in variables
}
}
}
答案 0 :(得分:1)
如果要通过索引设置文本字段的文本值,则应将它们添加到数组中,然后可以使用结果集的相同索引来设置文本。
let printers = [
printerName1,
printerName2,
printerName3,
printerName4,
printerName5
]
func load() {
let db = SQLiteDB.shared
db.openDB()
let data = db.query(sql: "SELECT * FROM tbPrinter")
for index in 0..<data.count {
let row = data[index]
if let printerName = row["name"] {
printers[index].text = printerName as? String
}
}
}