Core data操作含有初始数据的sqlite文件
时间:2010-08-17 来源:brucenan
原文地址在这里
原理其实很简单,就是把包含原始数据的文件包在项目文件里,
在第一次启动运行的时候,把它拷贝到resource目录下(不然在原目录内无法进行写入操作)。
代码 NSString *storePath = [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"FailedBanksCD.sqlite"];
// Put down default db if it doesn't already exist
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle]
pathForResource:@"FailedBanksCD" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
相关阅读 更多 +