mercredi 19 août 2015

Why saving data to SQLite database doesn't work with Swift 2?

I'm using SQLite.swift and I'm using these code from demo of SQLite.swift.

import UIKit
import SQLite

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let db = try! Connection()
        let users = Table("users")
        let id = Expression<Int64>("id")
        let email = Expression<String>("email")
        let name = Expression<String?>("name")
        try! db.run(users.create { t in
            t.column(id, primaryKey: true)
            t.column(email, unique: true, check: email.like("%@%"))
            t.column(name)
        })
        try! db.run(users.insert(email <- "alice@mac.com"))
        for user in db.prepare(users) {
            print("id: \(user[id]), email: \(user[email])")
        }
    }
}

The first time I ran it, the output was:

("SELECT * FROM \"users\"", [])
id: 1, email: alice@mac.com

Then I removed the line 17 ( try! db.run(users.insert(email <- "alice@mac.com")) ) and run again, debugger output changed to:

("SELECT * FROM \"users\"", [])

Looks like alice@mac.com didn't save to database. So where I did it wrong? Or how to save it into SQLite database?

P.S. I'm using Xcode 7 beta 5, Swift 2.0

Aucun commentaire:

Enregistrer un commentaire