Table not found after adding preloaded sqlite db to iOS App using Xcode
Below is the code which worked:
func copyDataBase() -> Bool { let fileManager = FileManager.default var dbPath = "" do { dbPath = try fileManager.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent(dbFileName).path } catch { print(error.localizedDescription) return false } if !fileManager.fileExists(atPath: dbPath) { let dbResourcePath = Bundle.main.path(forResource: "dbName", ofType: "sqlite") do { try fileManager.copyItem(atPath: dbResourcePath!, toPath: dbPath) } catch { print(error.localizedDescription) return false } } return true}
I hope this will help somebody in need
Firstly Create FileManager Object. Get Path and check file is exist or not then copy items from db. func callForCopyDBAllObejct() -> Bool { let fileM = FileManager.default var dbPath = nil do { dbPath = try fileM.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent(dbFileName).path } catch { print(error.localizedDescription) return false } if !fileM.fileExists(atPath: dbPath) { let dbResourcePath = Bundle.main.path(forResource: "dbName", ofType: "sqlite") do { try fileM.copyItem(atPath: dbResourcePath!, toPath: dbPath) } catch { print(error.localizedDescription) return false } } return true
}