FMDB简介及使用步骤

下载FMDB文件(gitHub链接:https://github.com/ccgus/fmdb)

  1 // 第一步: 引入的框架,引入支持的类库
  2 #import <FMDB.h>
  3 
  4 @interface ViewController ()
  5 
  6 // 声明数据库对象
  7 @property (nonatomic, strong) FMDatabase *dataBase;
  8 
  9 // 声明存储路径
 10 @property (nonatomic, copy) NSString *filePath;
 11 
 12 @end
 13 
 14 @implementation ViewController
 15 
 16 - (void)viewDidLoad {
 17     [super viewDidLoad];
 18     // Do any additional setup after loading the view, typically from a nib.
 19     // 创建表的方法
 20     [self createTable];
 21 }
 22 
 23 #pragma mark - 创建表
 24 - (void)createTable
 25 {
 26     // 第一步: 创建sqlite语句
 27     NSString *createSqlite = @"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)";
 28     
 29     // 第二步: 找到存储路径
 30     NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 31     NSLog(@"document = %@", document);
 32     self.filePath = [document stringByAppendingPathComponent:@"student.sqlite"];
 33     NSLog(@"filePath = %@", self.filePath);
 34     
 35     // 第三步: 使用路径初始化FMDB对象
 36     self.dataBase = [FMDatabase databaseWithPath:self.filePath];
 37     
 38     
 39     // 数据库执行相关操作
 40     // 需要判断数据库打开的时候才进行执行语句
 41     
 42     if ([self.dataBase open]) {
 43         BOOL result = [self.dataBase executeUpdate:createSqlite];
 44         if (result) {
 45             NSLog(@"创建表成功");
 46         } else {
 47             NSLog(@"创建表失败:result = %d", result);
 48         }
 49         
 50     }
 51     
 52     // 第五步: 关闭数据库
 53     [self.dataBase close];
 54     
 55 }
 56 
 57 
 58 #pragma 添加学生
 59 - (IBAction)insertIntoAction:(id)sender {
 60     
 61     // 第一步: 打开数据库
 62     [self.dataBase open];
 63     
 64     // 第二步: 进行相关的操作
 65     
 66     NSArray *nameArray = [NSArray arrayWithObjects:@"MBBoy", @"天天", @"小明", nil];
 67     
 68     for (int i = 0; i < nameArray.count; i++) {
 69         // 取出数组元素
 70         NSString *name = [nameArray objectAtIndex:i];
 71         // 插入语句
 72        //NSString *insertSqlite = @"insert into t_student(name,age,sex) values(?,?,?)";
 73      BOOL result = [self.dataBase executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)",name,@69,@"nan"];
 74         
 75         if (result) {
 76             NSLog(@"插入成功");
 77         } else {
 78             NSLog(@"插入失败: result = %d", result);
 79         }
 80 
 81     }
 82     
 83     // 关闭数据库
 84     [self.dataBase close];
 85     
 86 }
 87 
 88 #pragma 更改学生
 89 - (IBAction)updataAction:(id)sender {
 90     
 91     // 打来数据库
 92     [self.dataBase open];
 93  BOOL result  = [self.dataBase executeUpdate:@"update t_student set name = ? where name = ?",@"孟令旭", @"MBBoy"];
 94     if (result) {
 95         NSLog(@"更改成功");
 96     } else {
 97         NSLog(@"更改失败: result = %d", result);
 98     }
 99     
100     // 关闭数据库
101     [self.dataBase close];
102     
103 }
104 
105 
106 #pragma 删除学生
107 - (IBAction)deleteAction:(id)sender {
108     
109     // 打开数据库
110     [self.dataBase open];
111     
112     // 执行sql语句
113     BOOL result = [self.dataBase executeUpdate:@"delete from t_student where name = ?", @"孟令旭"];
114     if (result) {
115         NSLog(@"删除成功");
116     } else {
117         NSLog(@"删除失败: result = %d", result);
118     }
119     
120     // 关闭数据库
121     [self.dataBase close];
122     
123     
124 }
125 
126 
127 #pragma mark - 查询学生
128 - (IBAction)searchAction:(id)sender {
129     // 查询表中的所有数据库
130     // 打开数据库
131     [self.dataBase open];
132     // 查询结果使用的类FMResultSet
133     FMResultSet *reslutSet = [self.dataBase executeQuery:@"select * from t_student"];
134     
135     // 遍历出需要的结果内容
136     while ([reslutSet next]) {
137         NSString *name = [reslutSet objectForColumnName:@"name"];
138         NSInteger age = [reslutSet intForColumn:@"age"];
139         NSString *sex = [reslutSet objectForColumnName:@"sex"];
140         NSLog(@"name = %@, age = %ld, sex = %@", name,age,sex);
141     }
142     
143     // 关闭数据库
144     
145     [self.dataBase close];
146     
147 }
148 
149 #pragma mark - 插入很多学生
150 - (IBAction)insertMaryStudent:(id)sender {
151     
152     // 以队列的形式添加数据是FMDB比较常用的添加方式
153     
154     // FMDB不支持多个线程同时操作, 所以一般以串行的方式实现相关的操作
155     // 打开数据库
156     [self.dataBase open];
157     
158     // 第一步: 创建操作队列
159     FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.filePath];
160     // 标识: 记录是否操作成功
161     __block BOOL isSuccess = YES;
162     // 第二步: 把所需要的事件打包放在操作队列中
163     [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
164         
165         // 串行队列
166         isSuccess = [db executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)",@"女神", @"18", @""] && isSuccess;
167         isSuccess = [db executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)", @"black", @"18", @""] && isSuccess;
168         
169         isSuccess = [db executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)", @"男神", @"19", @""] && isSuccess;
170         
171         // 如果有错误,就会将它返回
172         if (!isSuccess) {
173             
174             // block 返回的参数rollback进行处理(bool类型的指针)
175             *rollback = YES;
176             return ;
177         }
178         
179     }];
180     // 关闭数据库
181     [self.dataBase close];
182     
183 }

 

posted @ 2016-05-30 16:09  雷坤  阅读(337)  评论(0编辑  收藏  举报