gpt4 book ai didi

ios - 无法从 iOS 应用程序打开 sqlite 数据库

转载 作者:行者123 更新时间:2023-11-29 12:18:53 25 4
gpt4 key购买 nike

我是 iOS 开发的新手,这是我第一次尝试使用 CoreData.framework

我用“sqlitebrowser”创建了一个非常简单的数据库,并将其复制到我的 XCode 项目中。我可以看到该文件包含在应用程序包中。我尝试访问 AppDelegate 类中的数据库,方法如下:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

//For now using the `SQLite` store generate manually. Make sure to open the
//store in read only mode since we cannot modify files in the bundle.

NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"MatchWatcher" withExtension:@"sqlite"];
//Should be like below
NSDictionary *storeOptions = @{NSReadOnlyPersistentStoreOption:@YES, NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"} };
// NSDictionary *storeOptions = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"} };
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:storeOptions
error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

return _persistentStoreCoordinator;
}

如您所见,我尝试使用包含 DB 的 ReadOnly 开口的 storeOptions 变量,但这给了我以下异常:

2015-07-06 14:15:18.372 MatchWatcher[27495:3849154] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite options:{ NSReadOnlyPersistentStoreOption = 1; NSSQLitePragmasOption = { "journal_mode" = DELETE; }; } ... returned error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x7fa38a73cbb0 {NSUnderlyingException=Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite} with userInfo dictionary { NSUnderlyingException = "Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite"; } 2015-07-06 14:15:18.373 StockWatcher[27495:3849154] Unresolved error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x7fa38a73cbb0 {NSUnderlyingException=Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite}, { NSUnderlyingException = "Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite"; }

对我做错了什么有什么想法吗?一些帮助将不胜感激!谢谢。

最佳答案

Core Data 使用的 SQLite 文件是以封闭方式构建的。它不适用于您在 Core Data 之外开发的 SQLite 文件。

Core Data 旨在成为您的应用程序的模型。将其视为关系数据库工具会给您带来问题。将它用作您的应用程序的模型,然后让它持久保存到磁盘。反之则不然。

关于ios - 无法从 iOS 应用程序打开 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com