概述sql
1. iOS开发中对数据进行本地缓存可谓屡见不鲜,小数据咱们用plist文件或者归档缓存便可,即简单又方便。但对于一些列表同样的数据(数据量比较大)就要用到数据库了。数据库
2. 关于数据库,移动开发中确定首选sqlite3。这是一款轻微型的嵌入式数据库,经过sql语句进行“增删改查”等数据操做。只是sqlite,里面公布的api都是一些纯c语言的代码,用起来繁琐不堪,极为痛苦。api
3. 而FMDB这个框架,就是对sqlite用oc语法进行了一层封装,咱们到时使用数据库时,就能够直接用面向对象的方式进行数据操做。缓存
1.FMDatabase – 表示一个单独的SQLite数据库。 用来执行SQLite的命令。安全
2.FMResultSet – 表示FMDatabase执行查询后结果集多线程
3.FMDatabaseQueue – 若是你想在多线程中执行多个查询或更新,你应该使用该类。这是线程安全的。框架
- (BOOL)executeUpdate:(NSString*)sql, ...; //能执行插入数据、删除数据、更新数据、建表删表操做。参数:传入要执行的sql语句dom
- (FMResultSet *)executeQuery:(NSString*)sql, ...; // 查询数据时用此方法。参数:传入要执行的sql语句。返回值:查询后结果集atom
注:其余的几个方法不一一例举,上面两个方法用于数据库操做,足矣!spa
使用时导入依赖库:libsqlite3.dylib,在须要用的地方导入头文件:"FMDB.h"
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //得到沙盒数据库文件路径,有这个文件直接得到,没有会进行建立 5 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"]; 6 7 // NSLog(@"%@",path); 8 9 // 1.建立数据库实例对象 10 self.db = [FMDatabase databaseWithPath:path]; 11 12 // 2.打开数据库 13 if ([self.db open]) { 14 NSLog(@"数据库打开成功"); 15 16 //建立一张学生表 17 BOOL result = [self.db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"]; 18 if (result) { 19 NSLog(@"创表成功!"); 20 } 21 22 } 23 24 25 } 26 27 - (IBAction)insert { 28 //插入数据 29 for (int i = 0; i < 20; i++) { 30 NSString *name = [NSString stringWithFormat:@"name%d",i]; 31 32 //注:此处sql语句具体的值能够用?替代,至关于占位符,后面逗号隔开后,放具体的值,如此能够防止数据写死 33 BOOL result =[self.db executeUpdate:@"insert into t_student (name,age) values (?,?);",name,@(i + 20)]; 34 if (result) { 35 NSLog(@"插入数据成功!"); 36 } 37 38 } 39 40 } 41 42 - (IBAction)delete { 43 //删除数据 44 45 [self.db executeUpdate:@"delete from t_student where age > ?;",@(30)]; 46 } 47 48 - (IBAction)update { 49 50 //更新数据 51 BOOL result = [self.db executeUpdate:@"update t_student set name = ?;",@"叶德雄"]; 52 53 if (result) { 54 NSLog(@"更新数据成功!"); 55 } 56 57 } 58 59 - (IBAction)select { 60 61 //查询数据 62 //返回一个FMResultSet集合,经过索引取数据,即调用其方法[set next],开始没有指向数据,返回no,调用一次指向下条数据,当最后跳数据指向完时,返回no,下面是经过while循环遍历数据 63 FMResultSet *set = [self.db executeQuery:@"select *from t_student where age > ?;",@(30)]; 64 while (set.next) { 65 //经过字段名字取数据 66 int ID = [set intForColumn:@"id"]; 67 NSString *name = [set stringForColumn:@"name"]; 68 int age = [set intForColumn:@"age"]; 69 70 NSLog(@"id=%d,name=%@,age=%d",ID,name,age); 71 } 72 }
执行数据操做时,用到的核心方法是:
- (void)inDatabase:(void (^)(FMDatabase *db))block;//block里面传回数据库实例FMDatabase *db,咱们用db对象进行增删改查操做
2 #import "FMDB.h" 3 4 @interface ViewController () 5 6 @property(nonatomic,strong)FMDatabaseQueue *queue; 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 14 [super viewDidLoad]; 15 16 //使用数据库队列操做数据,fmdb线程是不安全的,建议使用FMDatabaseQueue 17 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"sqlite.data"]; 18 //里面已经建立了FMDdataBase实例,数据库实例 19 self.queue = [FMDatabaseQueue databaseQueueWithPath:path]; 20 21 //经过block,拿到FMDatabase *db 22 [self.queue inDatabase:^(FMDatabase *db) { 23 //创表 24 BOOL result = [db executeUpdate:@"create table if not exists t_student(id integer primary key autoincrement,name text,age integer);"]; 25 if (result) { 26 NSLog(@"创表成功"); 27 } 28 }]; 29 30 } 31 32 - (IBAction)insert 33 34 { //经过block,拿到FMDatabase *db 35 [self.queue inDatabase:^(FMDatabase *db) { 36 for (int i = 0; i<40; i++) { 37 NSString *name = [NSString stringWithFormat:@"rose-%d", arc4random() % 1000]; 38 NSNumber *age = @(arc4random() % 100 + 1); 39 [db executeUpdate:@"insert into t_student (name, age) values (?, ?);", name, age]; 40 } 41 }]; 42 } 43 44 - (IBAction)update 45 { 46 [self.queue inDatabase:^(FMDatabase *db) { 47 51 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 52 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 53 54 55 - (IBAction)delete 71 {73 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 74 [db executeUpdate:@"update t_student set age = ? where name = ?;", @20, @"jack"]; 75 76} 82 83 - (IBAction)query 84 { 85 [self.queue inDatabase:^(FMDatabase *db) { 86 // 1.查询数据 87 FMResultSet *rs = [db executeQuery:@"select * from t_student where age > ?;", @50]; 88 89 // 2.遍历结果集 90 while (rs.next) { 91 int ID = [rs intForColumn:@"id"]; 92 NSString *name = [rs stringForColumn:@"name"]; 93 int age = [rs intForColumn:@"age"]; 94 95 NSLog(@"%d %@ %d", ID, name, age); 96 } 97 }]; 98 }