简体   繁体   English

从SQLite中检索列到表视图Swift iOS的单元格中的标签

[英]Retrieving a column from SQLite to a label in cell of table view Swift iOS

I am retrieving all the values of column formname to a titleLabel in a cell of a tableview, but unfortunately I am facing these two errors 我在tableview的单元格中检索列formname所有值到titleLabel ,但不幸的是我面临这两个错误

Call can throw, but it is not marked with 'try' and the error is not handled 调用可以抛出,但它没有标记为'try',并且没有处理错误

and this 还有这个

Call can throw, but it is not marked with 'try' and the error is not handled 调用可以抛出,但它没有标记为'try',并且没有处理错误

on these lines of below mentioned class. 在这些下面提到的类的行。

Here are other data model classes 以下是其他数据模型类

在此输入图像描述

import UIKit
import SQLite
import SQLite3

@available(iOS 11.0, *)
class ViewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    .appendingPathComponent("Stephencelis.sqlite")


let forms = Table("FormTable")

var cities : [String]?
var dbd: Connection?

var heroList = [FormDatabase]()
var db : OpaquePointer?
var stmt:OpaquePointer?

var formidV = Int64()
var formnameV = String()
var formdescriptionV = String()
var formdateV = String()

@IBOutlet weak var menu: UIBarButtonItem!
@IBOutlet weak var viewTableView: UITableView!
@IBOutlet weak var dateStringLabelView: UILabel!



private init() {
    let path = NSSearchPathForDirectoriesInDomains(
        .documentDirectory, .userDomainMask, true
        ).first!

    do {
        dbd = try Connection("\(path)/Stephencelis.sqlite3")
    } catch {
        dbd = nil
        print ("Unable to open database")
    }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}



func readValues(){

    heroList.removeAll()

    //this is our select query
    let queryString = "SELECT * FROM AuditorTable"

    //statement pointer
    var stmt:OpaquePointer?

    //preparing the query
    if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
        let errmsg = String(cString: sqlite3_errmsg(db)!)
        print("error preparing insert: \(errmsg)")
        return
    }

    //traversing through all the records
    while(sqlite3_step(stmt) == SQLITE_ROW){
        let formidC = sqlite3_column_int(stmt, 0)
        let formnameC = String(cString: sqlite3_column_text(stmt, 1))
        let formdescriptionC = String(cString: sqlite3_column_text(stmt, 2))
        let formcategoryC = String(cString: sqlite3_column_text(stmt, 3))
         let formdateC = String(cString: sqlite3_column_text(stmt, 4))

        //adding values to list
        heroList.append(FormDatabase(formid: Int64(formidC), formname: String(describing: formnameC), formdescription: String(describing: formdescriptionC), formcategory: String(describing: formcategoryC), formdate: String(describing: formdateC)))
    }
}

var numberofrowsinColumn = String()
func countValues(){

    //first empty the list of heroes
    //heroList.removeAll()

    //this is our select query
    let queryString = "SELECT COUNT(*) FROM AuditorTable"

    //statement pointer
    var stmt:OpaquePointer?

    //preparing the query
    if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
        let errmsg = String(cString: sqlite3_errmsg(db)!)
        print("error preparing insert: \(errmsg)")
        return
    }

    //traversing through all the records
    while(sqlite3_step(stmt) == SQLITE_ROW){
        let formidC = sqlite3_column_int(stmt, 0)
        let formnameC = String(cString: sqlite3_column_text(stmt, 1))
        let formdescriptionC = String(cString: sqlite3_column_text(stmt, 2))
        let formcategoryC = String(cString: sqlite3_column_text(stmt, 3))
        let formdateC = String(cString: sqlite3_column_text(stmt, 4))
        //adding values to list
        heroList.append(FormDatabase(formid: Int64(formidC), formname: String(describing: formnameC), formdescription: String(describing: formdescriptionC), formcategory: String(describing: formcategoryC), formdate: String(describing: formdateC)))
        numberofrowsinColumn = queryString
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   // return heroList.count
    if cities == nil {
        return 0
    }
    return cities!.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = viewTableView.dequeueReusableCell(withIdentifier: "ViewTableViewCell") as! ViewTableViewCell

    cell.titleLabel?.text = self.cities?[indexPath.row]



    cities = [String]()

    for row in (try dbd?.prepare(forms))! {

       try? cities!.append(row[0] as! String)





override func viewDidLoad() {
    super.viewDidLoad()


    if sqlite3_open(fileURL.path, &db) != SQLITE_OK {
        print("error opening database")
    }


    viewTableView.delegate = self
    viewTableView.dataSource = self
    viewTableView.reloadData()


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

You need to change your code to something like: 您需要将代码更改为:

let statement = try! dbd?.prepare(forms) 
for row in statement {
    if let city = row[0] as? String {
        cities?.append(city)
    }
}

You are using the method: 您正在使用该方法:

func prepare(_ query: QueryType) throws -> AnySequence<Row>

With this method you cannot use row[0] you need to use the method row.get(Expression<V>) which seems to be the name of the column. 使用此方法,您无法使用row[0]您需要使用方法row.get(Expression<V>) ,它似乎是列的名称。

The method you are trying to use is: 您尝试使用的方法是:

public func prepare(_ statement: String, _ bindings: Binding?...) throws -> Statement

With this function you can use row[0] 使用此功能,您可以使用row[0]

To be a little bit more clear you have two possibilities. 为了更清楚一点,你有两种可能性。

1 1

let forms = Table("FormTable")
...

cities = [String]()
if let connection = dbd {
    for row in try connection.prepare(forms) {
        // I'm not entirely sure about the way to use Expression
        let columnName = Expression<String>("COLUMN_NAME")
        try cities!.append(row.get(columnName))
    }
}

2 2

cities = [String]()
if let connection = dbd {
    for row in connection.prepare("SELECT * FROM FormTable") {
        try? cities!.append(row[0] as! String)
    }
}

I came to this solution by reading the source code. 我通过阅读源代码来解决这个问题。

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM