2
我將已填充的現有sqlite3文件複製到我的項目中;它被發現(我檢查與iPhone Core Data無法從Sqlite3中獲取任何數據
- (void)createEditableCopyOfDatabaseIfNeeded {
NSLog(@"Checking if we have to create editable copy of database");
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"hvw.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
NSLog(@"Creating editable copy of database");
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"hvw.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
我不來-part「數據庫的創建可編輯的副本」,因此該文件是存在的,發現的。
但我取不返回任何結果:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Game" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
不引發錯誤,但結果數組是空的...
編輯
我取代了線
NSString *documentsDirectory = [paths objectAtIndex:0];
通過
NSString *documentsDirectory = [self applicationDocumentsDirectory];
,並在日誌文件中我看到錯誤,當設法保存或閱讀對象:
Operation could not be completed. (Cocoa error 256.)
我認爲我的sqlite文件具有它所需要的結構... 我根據http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite -database-with-core-data-on-iphone-os-3-0/ – swalkner 2010-01-11 18:05:55
如果我理解這篇文章是正確的,他從Core Data創建了一個空白的SQLite數據庫,然後用手工填充它中的數據?這並不能保證您的元素的關係和其他數據將採用Core Data期望的格式。 – 2010-01-11 19:11:26
嗯......但我如何使用預先填充的數據庫呢? – swalkner 2010-01-11 19:14:42