您的位置:首页 > 数据库

IOS系列——数据库的操作

2014-06-14 14:19 127 查看

1。源文件数据库的移动

在有些项目中,工程本身带的有数据库,在app运行的时候,需要先吧数据库移动到沙盒文件中
- (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(@"数据库已经存在");
}
}
在这里,数据库的文件是city.db

移动的目标路径也可以这样写
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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: