6

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

Call can throw, but it is not marked with 'try' and the error is not handled

and this

Call can throw, but it is not marked with 'try' and the error is not handled

on these lines of below mentioned class.

Here are other data model classes

enter image description here

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.
}


}
Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52
iOS Developer
  • 311
  • 4
  • 25

2 Answers2

0

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)
    }
}
HereTrix
  • 1,315
  • 7
  • 13
0

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.

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]

To be a little bit more clear you have two possibilities.

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

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.

Xavier Bauquet
  • 712
  • 1
  • 6
  • 18