ccgus / fmdb

A Cocoa / Objective-C wrapper around SQLite
Other
13.84k stars 2.76k forks source link

Swift4.0.2 + FMDB2.7 beginTransaction is not work #631

Open onizine opened 6 years ago

onizine commented 6 years ago

Hi!

Please let me know if you know the cause. It worked in Xcode 8.2.1 & Swift 3. But with Xcode 9.1 & Swift 4.0.2 error: 7 "out of memory" occurs.

let db = FMDatabase(path)
db.open()
if db.beginTransaction(){
    //code
} else {
    //error!!!
    //code:7 "out of memory"
}
db.close()
robertmryan commented 6 years ago

Out of memory usually means that the open failed. Check that return value and see if it was true. Perhaps it's having trouble opening the path that you specified. Is it macOS or iOS? How did you build the path? We need more info to answer this question.

onizine commented 6 years ago

Thank you for your reply. Occurred in iOS application.(iOS11.1) SQlite DB file path No problem. db.open() is success. SQlite DB is placed in the document folder of the application. The transaction with FMDatabaseQueue succeeds on the same path. Indicates the code that worked normally.

if let queue:FMDatabaseQueue = FMDatabaseQueue(path: "SQLite Path"){
    queue.inTransaction { (dbq, rollback) in
        //code
    }
}

We will change it to implement FMDatabseQueue and avoid it. Please let me know if you know the solution.

Thank you

robertmryan commented 6 years ago

I don't know what to say because it works fine for me on both iOS 11.1 and 11.2 from Xcode 9.2 (9C40b):

let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)   // nowadays, I'd generally use `.applicationSupportDirectory`, but you said you were using Documents, so I did that, too
    .appendingPathComponent("test.sqlite")

let db = FMDatabase(url: fileURL)

guard db.open() else {
    print("unable to open db")
    return
}

defer { db.close() }

if db.beginTransaction() {
    try! db.executeUpdate("CREATE TABLE IF NOT EXISTS foo (bar TEXT)", values: nil)
    try! db.executeUpdate("INSERT INTO foo (bar) VALUES (?)", values: ["qux"])
    db.commit()
    let rs = try! db.executeQuery("SELECT * FROM foo", values: nil)
    while rs.next() {
        print("results:", rs.resultDictionary!)
    }
    print("done")
} else {
    print("code:", db.lastError(), "message:", db.lastErrorMessage())
}