jeudi 31 mars 2016

SQLite.swift using datetime

I'm using sqlite.swift for my database and I have a table that I would like to use a datetime column in.

currently I have a datamanager that helps me handle accessing the DB. So I have a class that has static constants like so:

static let itemId = Expression<Int>("itemId")
static let timeUpdateTime = Expression<String>("itemUpdateTime")

and I create the table like so:

static func createTable() throws {
    // create connection
    guard let DB = SQLiteDataStore.sharedInstance.BBDB else {
        throw DataAccessError.Datastore_Connection_Error
    }
    do {
        let _ = try DB.run(table.create(ifNotExists: true) { t in
            t.column(itemId, primaryKey: .Autoincrement)
            t.column(itemUpdateTime)
            })
    } catch _ {
        //error handling if table exists
    }
}

however I'm not sure how to filter only a range of datetime. e.g from 11am to 2pm. .filter() does not filter datetime and I can't filter a range of string as well. I know I can create an Expression<NSDate> for the table column, but it will be converted to a TEXT in the DB and I can't use that to get a range of datetime... How do I filter a range of datetime?

Aucun commentaire:

Enregistrer un commentaire