IOS系列——数据库的操作

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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值