Ios 如何使用swift 3 xcode 8在核心数据中预加载数据库
我有一个包含数据的数据库,我想在应用程序中预加载它。在swift 3之前,它是有效的,我遵循了本教程:但是如何为swift 3加载相同的数据库呢?随着Ios 如何使用swift 3 xcode 8在核心数据中预加载数据库,ios,swift,xcode,swift3,xcode8,Ios,Swift,Xcode,Swift3,Xcode8,我有一个包含数据的数据库,我想在应用程序中预加载它。在swift 3之前,它是有效的,我遵循了本教程:但是如何为swift 3加载相同的数据库呢?随着NSPersistentContainer的介绍,如何加载项目中的.sqlite文件?实际上,在swift 3中创建数据库的默认路径已更改。因此,现在代码将如下所示: func preloadDBData() { let sqlitePath = Bundle.main.path(forResource: "MyDB", ofType: "
NSPersistentContainer
的介绍,如何加载项目中的.sqlite文件?实际上,在swift 3中创建数据库的默认路径已更改。因此,现在代码将如下所示:
func preloadDBData() {
let sqlitePath = Bundle.main.path(forResource: "MyDB", ofType: "sqlite")
let sqlitePath_shm = Bundle.main.path(forResource: "MyDB", ofType: "sqlite-shm")
let sqlitePath_wal = Bundle.main.path(forResource: "MyDB", ofType: "sqlite-wal")
let URL1 = URL(fileURLWithPath: sqlitePath!)
let URL2 = URL(fileURLWithPath: sqlitePath_shm!)
let URL3 = URL(fileURLWithPath: sqlitePath_wal!)
let URL4 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite")
let URL5 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite-shm")
let URL6 = URL(fileURLWithPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite-wal")
if !FileManager.default.fileExists(atPath: NSPersistentContainer.defaultDirectoryURL().relativePath + "/MyDB.sqlite") {
// Copy 3 files
do {
try FileManager.default.copyItem(at: URL1, to: URL4)
try FileManager.default.copyItem(at: URL2, to: URL5)
try FileManager.default.copyItem(at: URL3, to: URL6)
print("=======================")
print("FILES COPIED")
print("=======================")
} catch {
print("=======================")
print("ERROR IN COPY OPERATION")
print("=======================")
}
} else {
print("=======================")
print("FILES EXIST")
print("=======================")
}
}
现在,您可以从AppDelegate的
didFinishLaunchWithOptions
方法调用此方法,这将预加载我们放入应用程序中的数据库。找到解决方案了吗?@LokeshChowdary是的。检查我添加了我的答案,效果非常好。Pooja:您完成了iOS 9.0支持的上述代码吗?@Pooja Shah您完成了吗ios 9.0的上述代码?如果是,请帮助我。@LokeshChowdary,Pooja:您在同一个项目()中使用过csv吗?我需要从csv获取数据并显示在表视图中。@pkc456您可以使用工具名:sqlite或mesa sql直接将csv文件转换为db,然后在应用程序中使用该db。@PoojaShah:嘿,我的应用程序崩溃了,因为在我的应用程序包中,我没有全部3个文件,而我从中提取了3个文件。实际上,我按照教程打印了文档路径,但在那里没有找到任何文件。。请帮帮我,先谢谢你。。!