无法将fmdb数据库获取到TableView中

时间:2019-04-27 12:09:23

标签: swift sqlite uitableview fmdb

我有数据到数据库中,但无法将所有数据加载到表视图中。

“ STUDENTDETAIL”是我的数据库名称,我无法填充表视图。

import UIKit
import FMDB

struct studentInfo {
    var name : String
    var Rollno : String
    var college : String
}

class customtablecell : UITableViewCell{

    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var lblRollno: UILabel!
    @IBOutlet weak var lblclg: UILabel!
}

class ShowStudentInTableView: UIViewController {

    @IBOutlet weak var tblView: UITableView!
     var studentinfo : [studentInfo]!
     var databasepath = String()

    override func viewDidLoad() {
        super.viewDidLoad()

          tblView.delegate = self
          tblView.dataSource = self

          navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))






    }

    override func viewWillAppear(_ animated: Bool) {
         self.selectdata()
        tblView.reloadData()

    }
    @objc func addTapped(){
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "AddStudentsDetail") as? AddStudentsDetail
        self.navigationController?.pushViewController(vc!, animated: true)

    }

    func selectdata(){
        //open database

        let filemanager = FileManager.default
        let dirpath = filemanager.urls(for: .documentDirectory, in: .userDomainMask)

        databasepath = dirpath[0].appendingPathComponent("STUDENTDETAIL.db").path

        if filemanager.fileExists(atPath: databasepath as String){

            let studentDB = FMDatabase(path: databasepath as String)

            if (studentDB.open()){


                let querySQL = "select name, rollno, college from STUDENTDETAIL"


                let results:FMResultSet? = studentDB.executeQuery(querySQL,withArgumentsIn: [])
                print("our database \(results!)")

                if (results?.next())!
                {

                    let student = studentInfo(name: (results?.string(forColumn: "name"))!, Rollno: (results?.string(forColumn: "rollno"))!, college: (results?.string(forColumn: "college"))!)
                    print("all data \(student)")

                    if studentinfo == nil
                    {
                        studentinfo = [studentInfo]()

                    }
                    studentinfo.append(student)
                    tblView.reloadData()
                }
                else
                {
                    print("Record not found")
                }
                studentDB.close()



            }

        }
        else {
            print("Error: )")
        }

    }

}

extension ShowStudentInTableView : UITableViewDelegate , UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return studentinfo.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customtablecell



        let allinfo = studentinfo[indexPath.row]
        cell.lblName.text = allinfo.name
        cell.lblRollno.text = allinfo.Rollno
        cell.lblclg.text = allinfo.college
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 131
    }

}

0 个答案:

没有答案