删除数据库条目swift SQLite

时间:2016-01-31 12:55:55

标签: ios database swift sqlite

是否有人能够告诉我如何在Xcode Swift中关闭模拟后自动删除所有数据库条目?我使用的数据库是SQLite。

我的代码如下;

import Foundation
import SQLite

class DBHQ {

    func StartDB(InputFirstName: String, InputLastName: String, InputEmail: String){
        let path = NSSearchPathForDirectoriesInDomains(
            .DocumentDirectory, .UserDomainMask, true
            ).first!

        let id = Expression<Int64>("id")
        let FirstName = Expression<String>("FirstName")
        let LastName = Expression<String?>("LastName")
        let Email = Expression<String?>("Email")
        let User = Table("User")

        //CREATING DATABASE
        do {
            let db = try Connection("\(path)/db.sqlite3")
            try db.run(User.create { t in
                t.column(id, primaryKey: true)
                t.column(FirstName, unique: true)
                t.column(LastName, unique: true)
                t.column(Email, unique: true)
                })

        } catch {

            do {
                let db = try Connection("\(path)/db.sqlite3")
                    //INPUT DATA
                    try db.run(User.insert(FirstName <- InputFirstName, LastName <- InputLastName, Email <- InputEmail))
                for user in try db.prepare(User) {
                    print("id:\(user[id]), FirstName: \(user[FirstName]), LastName: \(user[LastName]), Email: \(user[Email])")
                }
            } catch {
                print("Failiure")

        }
    }

}
}

干杯

0 个答案:

没有答案