Search code examples
iosswiftsqliteormdbaccess

Read SQLite database from local bundle using SharkORM


I am new in iOS development and I want to read database stored in app bundle with SharkORM. I found getCustomSettings() delegate but I think I'm using it wrong. It creates new DB somewhere else and it is empty.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, SRKDelegate {
private static let DatabaseName : String = "data"
private static let DatabaseType : String = "db"

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    SharkORM.setDelegate(self)
    SharkORM.openDatabaseNamed("data")
    return true
}

// MARK: SharkORM

func getCustomSettings() -> SRKSettings {
    let settings = SRKSettings()
    let databaseURL = URL(fileURLWithPath: Bundle.main.path(forResource: AppDelegate.DatabaseName, ofType: AppDelegate.DatabaseType)!)
    settings.databaseLocation = databaseURL.deletingLastPathComponent().path
    return settings
}
}

My database file is named data.db and it is stored in Resources/Database folder in project.

Can anybody tell me what is wrong? Can anybody provide me working example?


Solution

  • SharkORM will require R/W access to the database, the reality is that you will need to:

    1) Ensure it is a SharkORM compatible database file, e.g. all the property types, names and primary keys match the object model (must have Id PK column).

    2) Copy the database out of the bundle into a r/w directory on startup to use it. R/O is possible, but harder work to achieve.

    You could also use the rawExecute function to use an ATTACH command, if the tables are only to be referenced from within queries.