主页(http://www.zhonghuagame.com):iOS数据本地持久化
使用队列
需要根据bundle名创建上下文 创建多个数据库,即创建多个DataModel
查询示例:
提供了多线程安全的数据库操作方法,有效地防止数据混乱
// 利用沙盒根目录拼接字符串 NSString *homePath = NSHomeDirectory(); NSString *docPath = [homePath stringByAppendingString:@"/Documents"];第二种( !还??!)
4.更新
1 2 3 4 5 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.db"]; FMDatabase *database = [FMDatabase databaseWithPath:path]; if (![database open]) { NSLog(@"数据库打开失败!"); }
对比苹果自带的Core Data框架,更加轻量级和灵活
准备工作就是导入依赖库啦,在iOS中要使用SQLite3,需要添加库文件:libsqlite3.dylib并导入主头文件,这是一个C语言的库,所以直接使用SQLite3还是比较麻烦的。
应用沙盒目录的常见获取方式 沙盒根目录的获取方式正如上面我们所说:
sqlite3_finalize() : 释放stmt
1.字段类型
现在我们来看看应用沙盒里面这些文件夹都是做什么用的 添加或读取信息,需要根据不同的上下文,访问不同的实体integer : 整数
屏幕快照 2015-12-03 22.10.07.png
[myCache release];
到这里为止,就完成了全局变量的声明。
第三种( !~推荐~ !)
关联数据库和实体
3.打开数据库
之前的所有存储方法,都是覆盖存储。如果想要增加一条数据就必须把整个文件读出来,然后修改数据后再把整个内容覆盖写入文件。所以它们都不适合存储大量的内容。
(参考:) p4:SQLite/FMDBSQLite3
以上的所有存储方法,都是覆盖存储。如果想要增加一条数据就必须把整个文件读出来,然后修改数据后再把整个内容覆盖写入文件。所以它们都不适合存储大量的内容,大量存储需要用SQLite、CoreData等!
使用FMDatabase执行查询后的结果集
-(IBAction)addEmployee{ // 1. 创建两个部门 ios android //1.1 iOS部门 Department *iosDepart = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context]; iosDepart.name = @"ios"; iosDepart.departNo = @"0001"; iosDepart.createDate = [NSDate date]; //1.2 Android部门 Department *andrDepart = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context]; andrDepart.name = @"android"; andrDepart.departNo = @"0002"; andrDepart.createDate = [NSDate date]; //2. 创建两个员工对象 zhangsan属于ios部门 lisi属于android部门 //2.1 zhangsan Employee *zhangsan = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context]; zhangsan.name = @"zhangsan"; zhangsan.height = @(1.90); zhangsan.birthday = [NSDate date]; zhangsan.depart = iosDepart; //2.2 lisi Employee *lisi = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context]; lisi.name = @"lisi"; lisi.height = @2.0; lisi.birthday = [NSDate date]; lisi.depart = andrDepart; //3. 保存数据库 NSError *error = nil; [_context save:&error]; if (error) { NSLog(@"%@",error); } }读取信息 - Read
real : 实数(浮点数)
p5:CoreData
表面上SQLite将数据分为以下几种类型:
这里我只用上面的第三种方法!注意第一个参数!
前面说过一般不使用 sqlite3_exec() 方法查询数据。因为查询数据必须要获得查询结果,所以查询相对比较麻烦。示例代码如下:
sqlite3_step() : 逐行获取查询结果,不断重复,直到最后一条记录
<#NSSearchPathDirectory directory#> 这个参数代表要查找的文件,是个枚举! 枚举你懂的点击去看看就知道了~@interface AppDelegate : UIResponder { ASIDownloadCache *myCache; } @property (strong, nonatomic) UIWindow *window; @property (nonatomic,retain) ASIDownloadCache *myCache; 在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加如下代码 //自定义缓存 ASIDownloadCache *cache = [[ASIDownloadCache alloc] init]; self.myCache = cache; [cache release]; //设置缓存路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; [self.myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:]]; [self.myCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
在AppDelegate.m中的dealloc方法中添加如下语句
- (void)viewDidLoad { [super viewDidLoad]; /* * 关联的时候,如果本地没有数据库文件,Coreadata自己会创建 */ // 1. 上下文 NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; // 2. 上下文关连数据库 // 2.1 model模型文件 NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil]; // 2.2 持久化存储调度器 // 持久化,把数据保存到一个文件,而不是内存 NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; // 2.3 设置CoreData数据库的名字和路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"]; [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil]; context.persistentStoreCoordinator = store; _context = context; }