Paul
Paul

Reputation: 1431

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

Upvotes: 0

Views: 619

Answers (1)

wye
wye

Reputation: 346

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

Upvotes: 2

Related Questions