首先引入类库
声明数据库和路径
/** 声明数据库对象 */
@property (nonatomic, strong) FMDatabase *dataBase;
/** 声明存储路径 */
@property (nonatomic, strong) NSString *filePath;
创建表:
1 #pragma mark - 创建表 2 - (void)createTable { 3 4 // 1.创建sql语句 5 NSString *str = @"create table if not exists t_student (id integer primary key autoincrement not null, name text not null, age integer not null, sex text not null)"; 6 7 // 2.找到存储路径 8 NSString *document = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 9 self.filePath = [document stringByAppendingPathComponent:@"student.sqlite"]; 10 NSLog(@"filePath = %@", self.filePath); 11 12 // 3.初始化FMDB对象 13 self.dataBase = [FMDatabase databaseWithPath:self.filePath]; 14 15 // 4.判断数据库打开的时候才执行语句 16 if ([self.dataBase open]) { 17 BOOL result = [self.dataBase executeUpdate:str]; 18 19 if (result) { 20 NSLog(@"创建表成功"); 21 } else { 22 NSLog(@"创建表失败"); 23 } 24 } 25 26 // 5.关闭数据库 27 [self.dataBase close]; 28 }
添加
1 #pragma mark - 以队列方式插入多个学生【这种方式较为常用】 2 - (IBAction)insertManyStudent:(id)sender { 3 4 // FMDB不支持多个线程同时操作,所以一般以串行的方式实现相关的操作 5 6 // 1.打开数据库 7 [self.dataBase open]; 8 9 // 2.创建操作队列 10 FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.filePath]; 11 12 // 3.标识:记录是否操作成功 13 __block BOOL isSucced = YES; 14 15 // 4.把所需要的事件打包放在操作队列中 16 [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { 17 18 // 串行队列 19 isSucced = [db executeUpdate:@"insert into t_student (name, age, sex) values (?, ?, ?)", @"高月", @10, @"女"] && isSucced; 20 isSucced = [db executeUpdate:@"insert into t_student (name, age, sex) values (?, ?, ?)", @"石兰", @12, @"女"] && isSucced; 21 isSucced = [db executeUpdate:@"insert into t_student (name, age, sex) values (?, ?, ?)", @"卫庄", @34, @"男"] && isSucced; 22 23 // 如果有错误,就将它返回 24 if (!isSucced) { 25 // block返回的参数rollback进行处理(bool类型的指针) 26 *rollback = YES; 27 return; 28 } else { 29 NSLog(@"插入成功"); 30 } 31 }]; 32 33 // 5.关闭数据库 34 [self.dataBase close]; 35 }
更改
1 #pragma mark - 更改学生 2 - (IBAction)updateAction:(id)sender { 3 4 // 1.打开数据库 5 [self.dataBase open]; 6 7 // 2.执行语句 8 BOOL result = [self.dataBase executeUpdate:@"update t_student set name = ? where name = ?", @"少羽", @"卫庄"]; 9 if (result) { 10 NSLog(@"更改成功"); 11 } else { 12 NSLog(@"%d", result); 13 NSLog(@"更改失败"); 14 } 15 16 // 3.关闭数据库 17 [self.dataBase close]; 18 }
删除
1 #pragma mark - 删除学生 2 - (IBAction)deleteAction:(id)sender { 3 4 // 1.打开数据库 5 [self.dataBase open]; 6 7 // 2.执行sql语句 8 BOOL result = [self.dataBase executeUpdate:@"delete from t_student where name = ?", @"少羽"]; 9 if (result) { 10 NSLog(@"删除成功"); 11 } else { 12 NSLog(@"删除失败"); 13 } 14 15 // 3.关闭数据库 16 [self.dataBase close]; 17 }
查询所有
1 // 查询所有 2 - (NSMutableArray *)searchAll { 3 4 [self.dataBase open]; 5 FMResultSet *resultSet = [self.dataBase executeQuery:@"select *from p_person"]; 6 NSMutableArray *array = [NSMutableArray array]; 7 while ([resultSet next]) { 8 Person *person = [[Person alloc] init]; 9 person.id = [resultSet intForColumn:@"id"]; 10 person.name = [resultSet objectForColumnName:@"name"]; 11 person.age = [resultSet intForColumn:@"age"]; 12 person.weight = [resultSet doubleForColumn:@"weight"]; 13 14 [array addObject:person]; 15 } 16 [self.dataBase close]; 17 return array; 18 }