将外部准备好的sqlite导入到项目当中

时间:2014-06-02 18:28:36   收藏:0   阅读:282

首先,将sqlite数据库文件放在Resource文件夹下,并且允许其编译到项目当中。

之后在AppDelegate当中执行一些代码,这里将代码封装了一个Helper:

bubuko.com,布布扣
 1 #import "RPDBInitializeHelper.h"
 2 
 3 @implementation RPDBInitializeHelper
 4 
 5 + (void)initializeDatabase {
 6     NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/db.sqlite"];
 7     if (![[NSFileManager defaultManager] fileExistsAtPath:dbPath]) {
 8         NSString *dbResourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
 9         [[NSFileManager defaultManager] copyItemAtPath:dbResourcePath toPath:dbPath error:nil];
10     }
11 }
12 
13 @end
bubuko.com,布布扣

首先到沙盒的Documents目录下查找是否存在数据库文件,如果不存在,则从资源文件当中复制过去。

将外部准备好的sqlite导入到项目当中,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!