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];
相关文章推荐
- SQLXML系列之一:SQLXML初体验——用XML代替SQL来操作数据库
- 转-数据库应用基础系列-第2章 并发操作的一致性问题 (2)
- IOS数据库操作SQLite3使用详解
- iOS关于数据库操作之二 将工程里的数据库文件在程序加载时移至沙盒
- iOS关于数据库的操作(使用第三方类库FMDB)
- 使用JDOM操作XML系列----从数据库中读取记录集到层叠式XML文
- ZF1.*愤怒小鸟系列三:数据库操作之自动生成数据库连接配置
- ios上的数据库操作实例
- .NET RIA Service入门系列文章五:Silverlight 3操作数据库
- iOS 使用FMDB进行数据库操作
- IT民工系列——c#操作EditGrid,自己做一个在线Excel数据库吧!
- 高效的使用空间SQL操作空间数据库系列之四
- 高效的使用空间SQL操作空间数据库系列之三
- IOS数据库操作SQLite3使用详解
- Silverlight系列(4) 使用Web Service操作数据库
- 转-数据库应用基础系列-第2章 并发操作的一致性问题 (1)
- 高效的使用空间SQL操作空间数据库系列之二
- Silverlight与常用数据库互操作系列索引
- IOS 数据库操作SQLite3使用详解
- iOS 使用FMDB进行数据库操作