分享

ios关于数据库第三方框架FMDB

 quasiceo 2015-07-11

ios关于数据库第三方框架FMDB----基本用法

概述

1. iOS开发中对数据进行本地缓存可谓家常便饭,小数据我们用plist文件或者归档缓存即可,即简单又方便。但对于一些列表一样的数据(数据量比较大)就要用到数据库了。

2. 关于数据库,移动开发中肯定首选sqlite3。这是一款轻微型的嵌入式数据库,通过sql语句进行“增删改查”等数据操作。只是sqlite,里面公布的api都是一些纯c语言的代码,用起来繁琐不堪,极为痛苦。

3. 而FMDB这个框架,就是对sqlite用oc语法进行了一层封装,我们到时使用数据库时,就可以直接用面向对象的方式进行数据操作。

FMDB三个主要的类

1.FMDatabase – 表示一个单独的SQLite数据库。 用来执行SQLite的命令。

2.FMResultSet – 表示FMDatabase执行查询后结果集

3.FMDatabaseQueue – 如果你想在多线程中执行多个查询或更新,你应该使用该类。这是线程安全的。

FMDatabase执行数据库操作时用到的主要方法

- (BOOL)executeUpdate:(NSString*)sql, ...;  //能执行插入数据、删除数据、更新数据、建表删表操作。参数:传入要执行的sql语句

- (FMResultSet *)executeQuery:(NSString*)sql, ...; // 查询数据时用此方法。参数:传入要执行的sql语句。返回值:查询后结果集

 注:其他的几个方法不一一例举,上面两个方法用于数据库操作,足矣!

使用FMDatabase执行数据库操作

使用时导入依赖库: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 }
复制代码

 

上面提到过,直接使用FMDatabase线程是不安全的,在多线程进行数据库操作的情况下建议使用FMDatabaseQueue,详细用法如下:

执行数据操作时,用到的核心方法是:

- (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 }
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多