简体   繁体   English

无法在iOS中读取dtabase.sqlite文件(无此类表Z_METADATA)

[英]Not able to read dtabase.sqlite file in iOS (No such table Z_METADATA)

In new version I start using Core Data. 在新版本中,我开始使用Core Data。 But when I try to migrate from an old database to the new one, I'm getting an error: 但是,当我尝试从旧数据库迁移到新数据库时,出现错误:

let dbName = "db.sqlite:"

guard let modelURL = Bundle.main.url(forResource: "Ex", withExtension: "momd"), 
      let model = NSManagedObjectModel(contentsOf: modelURL) else {
    fatalError("model not found")
}

let psc = NSPersistentStoreCoordinator(managedObjectModel: model)
let databaseUrl = URL.defaultUrl.appendingPathComponent(dbName)

do {
    try psc.addPersistentStore(ofType: NSSQLiteStoreType, 
                    configurationName: nil, 
                                   at: databaseUrl, 
                              options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true])
} catch {
    fatalError("database migration error")
}

context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
context.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
context.persistentStoreCoordinator = psc

Error message: 错误信息:

 Error Domain=NSCocoaErrorDomain Code=256 "The file couldn't be opened." UserInfo={NSUnderlyingException=I/O error for database at /var/mobile/Containers/Data/Application/dsdasda-6Basdsd9C-4AE9-sad-sd/Documents/db.sqlite. SQLite error code:1, 'no such table: Z_METADATA', NSSQLiteErrorDomain=1} error: The file couldn't be opened. 

How i can resolve that? 我该如何解决?

The trailing colon in the dbName looks extraneous. dbName的结尾冒号看起来是多余的。 ("db.sqlite**:**") ( “db.sqlite **:**”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM