1。源文件数据库的移动
在有些项目中,工程本身带的有数据库,在app运行的时候,需要先吧数据库移动到沙盒文件中在这里,数据库的文件是city.db- (NSString *)getDBPath { // NSLog(@"^^^^^%@",[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/city.db"]); return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/city.db"]; } - (void)moveDB { //单例类 NSFileManager *fileManager = [NSFileManager defaultManager]; //源文件路径 NSString *srcPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"db"]; //目标文件路径 NSString *dstPath = [self getDBPath]; //当沙盒中不存在 才移动db if (![fileManager fileExistsAtPath:[self getDBPath]]) { //NO.1 知识点 //把工程列表中的数据库文件复制到沙盒中 if ([fileManager copyItemAtPath:srcPath toPath:dstPath error:nil]) { NSLog(@"数据库移动成功"); } else { NSLog(@"数据库移动失败"); } } else { NSLog(@"数据库已经存在"); } }
移动的目标路径也可以这样写NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *file = [path stringByAppendingPathComponent:@"data.rdb"];
2.数据库的操作
1.获取沙盒路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDic = [paths objectAtIndex:0]; NSString *path = [documentsDic stringByAppendingPathComponent:SQLNAME];