【发布时间】:2019-10-15 09:46:33
【问题描述】:
我在 sqlite 文件中有大型数据库 所以我试图直接从我的 data.sqlite 文件中访问它 但我无法访问它。我过去使用目标 C 完成了该任务
采取的步骤:
1:将data.sqlite文件拖入项目中
2:尝试通过 fmdb 访问它
输出:路径=== /var/mobile/Containers/Data/Application/557B2961-52D8-4B14-BABC-BF4829852127/Documents/data.sqlite
let documentsDirectory = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString) as String
pathToDatabase = documentsDirectory.appending("/(databaseFileName)")
func openDatabase() -> Bool {
print("path=== \(pathToDatabase ?? "empty")")
if database == nil {
if FileManager.default.fileExists(atPath: pathToDatabase) {
database = FMDatabase(path: pathToDatabase)
//code is not entering in that if , it should be there becasuse I already included my sqlitefile
}
if database != nil {
if database.open() {
return true
}
}
return false
}
【问题讨论】:
-
显示您如何设置
pathToDatabase。您是否确认您的data.sqlite在构建应用程序时实际上被复制到应用程序的捆绑包中? -
让documentsDirectory = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString) as String pathToDatabase = documentsDirectory.appending("/(databaseFileName)")
-
这需要在您的问题中,而不是在评论中。
-
请指导我如何确认 data.sqlite 文件实际上正在被复制到应用程序包中?
-
查看stackoverflow.com/a/51054075/1226963 了解如何检查文件的目标成员。