IOS 數(shù)據(jù)存儲之 FMDB 詳解
FMDB是用于進(jìn)行數(shù)據(jù)存儲的第三方的框架,它與SQLite與Core Data相比較,存在很多優(yōu)勢。
FMDB是面向?qū)ο蟮?,它以O(shè)C的方式封裝了SQLite的C語言API,使用起來更加的方便,不需要過多的關(guān)心數(shù)據(jù)庫操作的知識。但是它本身也存在一些問題,比如跨平臺,因?yàn)樗怯胦c的語言封裝的,所以只能在ios開發(fā)的時(shí)候使用,如果想實(shí)現(xiàn)跨平臺的操作,來降低開發(fā)的成本和維護(hù)的成本,就需要使用比較原始的SQLite。
Core Data是ORM的一種體現(xiàn),使用Core Data需要用到模型數(shù)據(jù)的轉(zhuǎn)化,雖然操作簡單,不需要直接操作數(shù)據(jù)庫,但是性能沒有直接使用SQLite高。但是SQLite使用的時(shí)候需要使用c語言中的函數(shù),操作比較麻煩,因此需要對它進(jìn)行封裝。但是如果只是簡單地封裝,很可能會忽略很多重要的細(xì)節(jié),比如如何處理并發(fā)以及安全性更問題。
因此,在這里推薦使用第三方框架FMDB,它是對libsqlite3框架的封裝,用起來的步驟與SQLite使用類似,并且它對于多線程的同時(shí)操作一個(gè)表格時(shí)進(jìn)行了處理,也就意味著它是線程安全的。FMDB是輕量級的框架,使用靈活,它是很多企業(yè)開發(fā)的首選。
FMDB中重要的類
FMDatabase:一個(gè)FMDatabase對象就代表一個(gè)單獨(dú)的SQLite數(shù)據(jù)庫,用來執(zhí)行SQL語句
FMResultSet:使用FMDatabase執(zhí)行查詢后的結(jié)果集
FMDatabaseQueue:用于在多線程中執(zhí)行多個(gè)查詢或更新,它是線程安全的
FMDB使用步驟
1. 下載FMDB文件 fmdb下載地址 ,將FMDB文件夾添加到項(xiàng)目中
2. 導(dǎo)入sqlite框架,導(dǎo)入FMDatabase.h文件
3.與SQLite使用步驟類似,需要獲取數(shù)據(jù)庫文件路徑,然后獲得數(shù)據(jù)庫,并打開數(shù)據(jù)庫,然后數(shù)據(jù)庫進(jìn)行操作,最后關(guān)閉數(shù)據(jù)庫。代碼如下所示:
- // ViewController.m
- // JRFMDB
- //
- // Created by jerehedu on 15/6/18.
- // Copyright (c) 2015年 jerehedu. All rights reserved.
- //
- #import "ViewController.h"
- #import "FMDatabase.h"
- @interface ViewController ()
- @property (nonatomic, strong) FMDatabase *db;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //導(dǎo)入sqlite框架,導(dǎo)入FMDB文件夾
- //1.獲得數(shù)據(jù)庫文件的路徑
- NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
- NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"];
- NSLog(@"fileName = %@",fileName);
- //2.獲得數(shù)據(jù)庫
- FMDatabase *db = [FMDatabase databaseWithPath:fileName];
- //3.打開數(shù)據(jù)庫
- if ([db open]) {
- NSLog(@"ok");
- //4.創(chuàng)表
- BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
- if (result) {
- NSLog(@"創(chuàng)表成功");
- }else{
- NSLog(@"創(chuàng)表失敗");
- }
- }
- self.db=db;
- //插入數(shù)據(jù)
- [self insertStu];
- [self deleteStu:6];
- [self updateStu:@"apple7_name" :@"7777"];
- [self queryStu];
- [self dropStu];
- [self insertStu];
- [self queryStu];
- //6.關(guān)閉數(shù)據(jù)庫
- [self.db close];
- }
- #pragma mark 插入數(shù)據(jù)
- -(void)insertStu
- {
- for (int i=0; i<10; i++)
- {
- NSString *name = [NSString stringWithFormat:@"1apple%i_name",i];
- int age = arc4random()%3+20;
- //1. executeUpdate : 不確定的參數(shù)用?來占位 (后面參數(shù)必須都是oc對象)
- [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)];
- //2. executeUpdateWithFormat : 不確定的參數(shù)用%@、%d等來占位 (參數(shù)為原始數(shù)據(jù)類型)
- // [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age];
- //3. 數(shù)組
- // [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]];
- }
- }
- #pragma mark 刪除數(shù)據(jù)
- -(void)deleteStu:(int)idNum
- {
- //a. executeUpdate : 不確定的參數(shù)用?來占位 (后面參數(shù)必須都是oc對象)
- // [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)];
- //b. executeUpdateWithFormat : 不確定的參數(shù)用%@、%d等來占位
- // [self.db executeUpdateWithFormat:@"delete from t_student where name=%@;",@"apple9_name"];
- }
- #pragma mark 銷毀表格
- -(void)dropStu
- {
- [self.db executeUpdate:@"drop table if exists t_student;"];
- //4.創(chuàng)表
- BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
- if (result) {
- NSLog(@"再次創(chuàng)表成功");
- }else{
- NSLog(@"再次創(chuàng)表失敗");
- }
- }
- #pragma mark 修改數(shù)據(jù)
- -(void)updateStu:(NSString *)oldName :(NSString*)newName
- {
- // [self.db executeUpdateWithFormat:@"update t_student set name=%@ where name=%@;",newName,oldName];
- [self.db executeUpdate:@"update t_student set name=? where name=?",newName,oldName];
- }
- #pragma mark 查詢數(shù)據(jù)
- -(void)queryStu
- {
- //1.執(zhí)行查詢語句
- // FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"];
- FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)];
- //2.遍歷結(jié)果集合
- while ([resultSet next]) {
- int idNum = [resultSet intForColumn:@"id"];
- NSString *name = [resultSet objectForColumnName:@"name"];
- int age = [resultSet intForColumn:@"age"];
- NSLog(@"id=%i ,name=%@, age=%i",idNum,name,age);
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end