小智 5
如果您已创建.sqlite文件并在其中包含表,则将.sqlite文件添加到xcode中,就像从桌面拖动到您的包中一样(参考资料).然后使用NSFileManager访问sqlite文件.您需要编写方法对于createdatabase和initialize database,您还可以在文档文件夹/模拟器文件夹中看到sqlite文件.
- (void)createEditableCopyOfDatabaseIfNeeded {
NSLog(@"Checking for database file");
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ihaikudb.sql"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
NSLog(@"If needed, bundled default DB is at: %@",defaultDBPath);
if(!success) {
NSLog(@"Database didn't exist... Copying default from resource dir");
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
} else {
NSLog(@"Database must have existed at the following path: %@", dbPath);
}
NSLog(@"Done checking for db file");
}
Run Code Online (Sandbox Code Playgroud)