每列都有最后一列的值。 SQLITE数据库

时间:2019-01-18 12:54:58

标签: ios swift database sqlite

该行中每一列的输出都相同。

我的输出应类似于

  

“杰克1爆炸男性”

但是我变得像

  

“男性1男性男性”

每列都有最后一列的值。 我的整个代码在下面列出。

请帮助我成为sqlite的新手。

我不知道应该在哪里更改代码以使其正确无误,请帮忙。

import UIKit
import SQLite3

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var lab1: UILabel!

    var db: OpaquePointer?
    var heroList = [Hero]()

    @IBOutlet weak var tableViewHeroes: UITableView!


    @IBAction func buttonSave(_ sender: UIButton) {
        let name = "jack"
        let powerRanking = "1"
        let power = "blast"
        let gender = "Male"




        var stmt: OpaquePointer?

        let queryString = "INSERT INTO Heroes (name, powerrank, power, gender) VALUES (?,?,?,?)"

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

        if sqlite3_bind_text(stmt, 1, name, -1, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")
            return
        }

        if sqlite3_bind_int(stmt, 2, (powerRanking as NSString).intValue) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")
            return
        }

        if sqlite3_bind_text(stmt, 3, power, -1, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")
            return
        }

        if sqlite3_bind_text(stmt, 4, gender, -1, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")
            return
        }




        if sqlite3_step(stmt) != SQLITE_DONE {
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure inserting hero: \(errmsg)")
            return
        }



        readValues()

        print("Herro saved successfully")
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return heroList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
        let hero: Hero
        hero = heroList[indexPath.row]
        cell.textLabel?.text = hero.id.description + (hero.name?.description)! + (hero.powerRanking.description) + (hero.power?.description)! + (hero.gender?.description)!

        return cell
    }


    func readValues(){
        heroList.removeAll()

        let queryString = "SELECT * FROM Heroes ORDER BY id DESC LIMIT 1;"

        var stmt:OpaquePointer?

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

        while(sqlite3_step(stmt) == SQLITE_ROW){
            let id = sqlite3_column_int(stmt, 0)
            let name = String(cString: sqlite3_column_text(stmt, 1))
            let powerrank = sqlite3_column_int(stmt, 2)

            let power = String(cString: sqlite3_column_text(stmt, 3))
            let gender = String(cString: sqlite3_column_text(stmt, 4))


            heroList.append(Hero(id: Int(id), name: String(name), powerRanking: Int(powerrank),  power: String(power),  gender: String(gender)))
        }

        self.tableViewHeroes.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()


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


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

        if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Heroes (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), powerrank INTEGER, power VARCHAR(255), gender VARCHAR(255))", nil, nil, nil) != SQLITE_OK {
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("error creating table: \(errmsg)")
        }

        readValues()
    }
}

1 个答案:

答案 0 :(得分:0)

通过对字符串进行编码将解决问题

if sqlite3_bind_text(stmt, 2, (monthen as NSString).utf8String, -1, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")

            return
        }