H
H
Hirako7972018-10-19 18:22:53
SQLite
Hirako797, 2018-10-19 18:22:53

How to fix error when connecting SQLite database in Swift?

When using SQLite.Swift to work with the application's SQLite database is built. But, after being immersed in the simulator, it crashes. I can't figure out what the problem is. Tell me who can?
Here is the code:

import Foundation
import SQLite

class Database {
    static let shared = Database()
    public let connection: Connection?
    private init() {
        let dbPath = Bundle.main.path(forResource: "sqliteExample", ofType: "db")! <i>// вот здесь ошибка!</i>
        do {
            connection = try Connection(dbPath, readonly: true)
         } catch {
            connection = nil
            let nserror = error as NSError
            print ("Cannot connect to Database. Error is: \(nserror), \(nserror.userInfo)")
        }
    }
}

Error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Example[19989:2210978] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
The working option is commented out on the screen, but unfortunately I can’t connect the database I need.5bc9f6bd1f65f381025137.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hirako797, 2018-10-20
@Hirako797

Found a solution. The question is closed.
import Foundation
import SQLite
class Database {
static let shared = Database()
public let connection: Connection?
public let dbFileName = "Example.db"
private init() {
let dbPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first as String!
do {
connection = try Connection("\(dbPath!)/Example.db")
} catch {
connection = nil
let nserror = error as NSError
print ("Cannot connect to Database. Error is: \(nserror), \(nserror .userInfo)")
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question