swiftsqlitesqlite.swift

Cannot create sqlite file with SQLite.swift "cannot open file at line..."


I'm attempting to create a sqlite DB file within my iOS project. I'm using the code as documented for R/W

let path = NSSearchPathForDirectoriesInDomains(
    .documentDirectory, .userDomainMask, true
).first!

let db = try Connection("\(path)/db.sqlite3")

but I end up with a cannot open file at line 45340 of [d24547a13b].

The closest resource I've found is Why do I get Unable to Open Database file? but the code there seems to be the same as what I have.

edit: More logs

[logging-persist] os_unix.c:45340: (0) open
- Undefined error: 0

Solution

  • DB file needs to be created under the documents directory. See Brando Flores' answer: https://stackoverflow.com/a/70514807/346676

    do {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let dbFile = "\(path.first ?? "")/db.sqlite3"
        print(dbFile)   // show full file path
        db = try Connection(dbFile)
    }
    catch {
        print(error)
    }