Hello everyone. I have the following problem. I have an application that queries a database in sqlite. When I run the app in xcode on my mac I give it the path to the database to make it work. So far so good. But when I want to test this application on a physical device (iPhone 11), I get the error that the application cannot find the database. My question or problem is where I have to store the database on the iPhone so that the application can make the queries. Thank you.
What Scott said but also, I think this is your main problem:
I generate the database connection code, which is this
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) .appendingPathComponent("TurMer.sqlite")
You have set things up so that your database is bundled in your app. That means that you won’t be able to find it in the Documents directory. Rather, you need to use Bundle
. Try this:
let fileURL = Bundle.main.url(forResource: "TurMer", withExtension: "sqlite")!
Note the force unwrap (!
) at the end. This is standard practice for this API when accessing items within your own bundle. This will trap if the item isn’t present. This is exactly what you want in this case because, if the item isn’t present, it means that your app was built incorrectly.
And remember that your bundle is read-only so, as Scott suggested, you want to open it with SQLITE_OPEN_READONLY
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"