读取现有sqlite-Swift
我在我的项目中有一个现有的读取现有sqlite-Swift,swift,sqlite,core-data,Swift,Sqlite,Core Data,我在我的项目中有一个现有的Sqlite,我将其复制到我的设备中,如下面所示: func prepareDatabaseFile() /*-> String*/ { let fileName: String = "myDB.sqlite" let fileManager:FileManager = FileManager.default let directory = fileManager.urls(for: .documentDirectory, in: .us
Sqlite
,我将其复制到我的设备中,如下面所示:
func prepareDatabaseFile() /*-> String*/ {
let fileName: String = "myDB.sqlite"
let fileManager:FileManager = FileManager.default
let directory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let documentUrl = directory.appendingPathComponent(fileName)
let bundleUrl = Bundle.main.resourceURL?.appendingPathComponent(fileName)
// here check if file already exists on simulator
if fileManager.fileExists(atPath: (documentUrl.path)) {
//Document file exists!
//return documentUrl.path
}else if fileManager.fileExists(atPath: (bundleUrl?.path)!) {
//Document file does not exist, copy from bundle!
try! fileManager.copyItem(at:bundleUrl!, to:documentUrl)
}
}
我有一个名为ProjectCoreDataStore
的class
,我用这个class
获得了sqlite
的所有结果,像下面这样的class
我如何用下面的class来定制我的existsqlite
class ProjectCoreDataStore: DegreesProtocol, DegreesStoreUtilityProtocol {
// MARK: - Managed object contexts
var mainManagedObjectContext: NSManagedObjectContext
var privateManagedObjectContext: NSManagedObjectContext
// MARK: - Object lifecycle
init()
{
// This resource is the same name as your xcdatamodeld contained in your project.
guard let modelURL = Bundle.main.url(forResource: "myDB", withExtension: "momd") else {
fatalError("Error loading model from bundle")
}
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
guard let mom = NSManagedObjectModel(contentsOf: modelURL) else {
fatalError("Error initializing mom from: \(modelURL)")
}
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
mainManagedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
mainManagedObjectContext.persistentStoreCoordinator = psc
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docURL = urls[urls.endIndex-1]
/* The directory the application uses to store the Core Data store file.
This code uses a file named "DataModel.sqlite" in the application's documents directory.
*/
let storeURL = docURL.appendingPathComponent("myDB.sqlite")
do {
try psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: nil)
} catch {
fatalError("Error migrating store: \(error)")
}
privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateManagedObjectContext.parent = mainManagedObjectContext
}
deinit
{
do {
try self.mainManagedObjectContext.save()
} catch {
fatalError("Error deinitializing main managed object context")
}
}
func myQuery(){
........
}
}
问题:当我转到复制sqlite的路径时,我看到sqlite是空的 不确定您在问什么,您问的是
ProjectCoreDataStore
中的代码是否正常工作,您问的是您的型号,还是发布的代码有问题?@Joakim Danielson。如何使用ProjectCoreDataStore自定义exist sqlite?当我转到copy sqlite的路径时,我看到sqlite是空的。我怀疑您还需要复制WAL文件。对于使用Sqlite的WAL模式的iOS,数据存储在两个单独的文件中,即主.Sqlite文件和一个单独的.Sqlite WAL文件。您需要同时复制这两个文件。您的myDb.sqlite是使用核心数据创建的吗?如果不是,您就不能将其直接用于核心数据,您需要首先迁移它。这就是苹果写的关于使用现有SQLite数据库的内容,除非您将现有SQLite数据库导入核心数据存储,否则您不会这样做。尽管核心数据支持SQLite作为其持久存储类型之一,但数据库格式是私有的。您不能使用本机SQLite API创建SQLite数据库并将其直接用于核心数据。如果您有一个现有的SQLite数据库,则需要将其导入核心数据存储。