I'm trying to learning swiftUi and I've included SQLIte in my project. I've successfully created db, tables and func to read and insert records and I tested it in Xcode emulator and all works fine. I'm using Xcode 12.1. When I try to launch my app to real device (iphone 11, software version 13.5.1), the db isn't create and I get this error:
2021-04-14 18:00:59.296690+0200 playToys[285:7409] [logging-persist] cannot open file at line 43353 of [378230ae7f] 2021-04-14 18:00:59.296742+0200 playToys[285:7409] [logging-persist] os_unix.c:43353: (0) open(/var/mobile/Containers/Data/Application/3D858B04-DA3A-488E-804A-DFD13DD882E3/Documents.playToysDb.sqlite)
- Undefined error: 0 There is error in creating DB puntamento creazionefile:///var/mobile/Containers/Data/Application/3D858B04-DA3A-488E-804A-DFD13DD882E3/Documents.playToysDb.sqlite/ 2021-04-14 18:00:59.298066+0200 playToys[285:7409] [logging] API call with NULL database connection pointer 2021-04-14 18:00:59.298104+0200 playToys[285:7409] [logging] misuse at line 131400 of [378230ae7f] Prepration table cliente fail 2021-04-14 18:00:59.298127+0200 playToys[285:7409] [logging] API call with NULL database connection pointer 2021-04-14 18:00:59.298142+0200 playToys[285:7409] [logging] misuse at line 131400 of [378230ae7f] Prepration createing Currencies table fail 2021-04-14 18:00:59.298161+0200 playToys[285:7409] [logging] API call with NULL database connection pointer 2021-04-14 18:00:59.298174+0200 playToys[285:7409] [logging] misuse at line 131400 of [378230ae7f] Prepration createing Categorie table fail
This is a part of code to create and open db:
class DBHelper {
static let dbHelperInstance = DBHelper()
var db : OpaquePointer?
var path : String = "playToysDb.sqlite"
private init() {}
func initDatabase(){
self.db = createDB()
self.createTableCurrency()
self.initDefaultItems()
}
func createDB() -> OpaquePointer? {
let filePath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathExtension(path)
var dbPointer : OpaquePointer? = nil
if sqlite3_open(filePath.path, &dbPointer) != SQLITE_OK {
print("There is error in creating DB")
return nil
}else {
print("Database has been created with path \(path)")
return dbPointer
}
}
func openDatabase()
{
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathExtension(path)
if sqlite3_open(fileURL.path, &db) == SQLITE_OK {
print("Database Created !!")
}
}
}
Any idea? Thank you