jeudi 24 septembre 2015

sqlite.swift does not seem to commit to disk

Trying to use sqlite.swift (swift-2 version) for a simple IOS app (targeting IOS 9). I'm creating a database / connecting to an existing database using:

func connectDB() -> Bool {

    let path = NSSearchPathForDirectoriesInDomains(
        .DocumentDirectory, .UserDomainMask, true
        ).first!

    let db = try? Connection("\(path)/db.sqlite3")
    return (db != nil)
}

I can see the database being created on disk (in the simulator), db.sqlite3, Zero bytes.

Now when I create a table using:

func createTableVehiclesAndFillFromAPI() -> Bool {
    let vehicles = Table("vehicles")
    let id = Expression<Int64>("id")
    let name = Expression<String?>("name")
    let model = Expression<String>("model")
    let manufacturer = Expression<String>("manufacturer")

    do {
    try db.run(vehicles.create (ifNotExists: true) { t in
        t.column(id, primaryKey: true)
        t.column(name)
        t.column(model)
        t.column(manufacturer)
        })          
    } catch {
       print ("create table vehicles FAILED")
    }

and try to insert 10 rows using:

let insert = vehicles.insert(
    name <- item ["name"].stringValue,
    model <- item ["model"].stringValue,
    manufacturer <- item ["manufacturer"].stringValue)

let _ = try! db.run(insert)

Finally calling let count = try db.scalar(vehicles.count) , which outputs 10, so table creation and insertion have worked as expected. However, the size of the sqlite database on disk does NOT change. Furthermore, next time I run my app, the table will be re-created again, as if the database is empty again.

Am I missing something obvious here?

Aucun commentaire:

Enregistrer un commentaire