FMDB的使用
#import "ViewController.h" #import "FMDatabase.h" #import "FMDatabaseQueue.h" //使用多线程时导入 @interface ViewController () @property (nonatomic, strong) FMDatabase *db; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self create]; } #pragma - 建立表格 - (IBAction)create { //1.获得数据库文件的路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; doc = @"/Users/apple/Desktop/数据库"; NSString *fileName = [doc stringByAppendingPathComponent:@"student.sqlite"]; //2.获得数据库 FMDatabase *db = [FMDatabase databaseWithPath:fileName]; self.db = db; //3.使用如下语句,如果打开失败,可能是权限不足或者资源不足。通常打开完操作操作后,需要调用 close 方法来关闭数据库。在和数据库交互 之前,数据库必须是打开的。如果资源或权限不足无法打开或创建数据库,都会导致打开失败。 if ([db open]) { //4.建表 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(@"建表成功!"); }else { NSLog(@"建表失败!"); } } } #pragma - 插入数据 - (IBAction)insert { NSString *name = @"张三"; int age = 27; //方式1.executeUpdate:不确定的参数用 ?来占位(后面参数必须是oc对象,;代表语句结束) [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)]; //方式2.executeUpdateWithForamat:不确定的参数用%@,%d等来占位 (参数为原始数据类型,执行语句不区分大小写) [self.db executeUpdateWithFormat:@"insert into t_student (name,age) values (%@,%i);",name,age]; //方式3.参数是数组的使用方式 [self.db executeUpdate:@"INSERT INTO t_student(name,age) VALUES (?,?);"withArgumentsInArray:@[name,@(age)]]; } #pragma - 删除数据 - (IBAction)delete { //方式1.不确定的参数用 ?来占位(后面参数必须是oc对象,需要将int包装成OC对象) int idNum = 1; [self.db executeUpdate:@"delete from t_student where ID = ?;",@(idNum)]; NSString *name = @"张三"; //方式2.不确定的参数用%@,%d等来占位 [self.db executeUpdateWithFormat:@"delete from t_student where name = %@;",name]; } #pragma - 修改数据 - (IBAction)update { NSString *oldName = @"张三"; NSString *newName = @"李四"; //修改学生的名字 [self.db executeUpdate:@"update t_student set name = ? where name = ?",newName,oldName]; } #pragma - 查询数据 - (IBAction)select { /* FMResultSet获取不同数据格式的方法: intForColumn: longForColumn: longLongIntForColumn: boolForColumn: doubleForColumn: stringForColumn: dataForColumn: dataNoCopyForColumn: UTF8StringForColumnIndex: objectForColumn: */ //查询整个表 FMResultSet *resultSet1 = [self.db executeQuery:@"select * from t_student;"]; //根据条件查询 FMResultSet *resultSet2 = [self.db executeQuery:@"select * from t_student where id<?;",@(14)]; //遍历结果集合 while ([resultSet2 next]) { int idNum = [resultSet2 intForColumn:@"ID"]; NSString *name = [resultSet2 objectForColumnName:@"name"]; int age = [resultSet2 intForColumn:@"age"]; } } #pragma - 销毁命令 - (IBAction)drop { //如果表格存在 则销毁 [self.db executeUpdate:@"drop table if exists t_student;"]; } #pragma - 多线程的使用方法 - (void)method { NSString *aPath = @"......"; //1.创建队列 FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; __block BOOL whoopsSomethingWrongHappened = true; //2.把任务包装到事务里 [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { whoopsSomethingWrongHappened &= [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]]; whoopsSomethingWrongHappened &= [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]]; whoopsSomethingWrongHappened &= [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]]; //如果有错误 返回 if (!whoopsSomethingWrongHappened) { *rollback = YES; return; } }]; } @end