dimanche 31 janvier 2016

Delete database entries swift SQLite

Is anyone able to show me how i can automatically delete all database entries after closing the simulation in Xcode Swift? The database i am using is SQLite.

My code is as follows;

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")

    }
}

} }

Cheers

Aucun commentaire:

Enregistrer un commentaire