IOS 数据存储之 FMDB 详解

日期:2015-07-16点击次数:2020

       FMDB是用于进行数据存储的第三方的框架,它与SQLite与Core Data相比较,存在很多优势。

       FMDB是面向对象的,它以OC的方式封装了SQLite的C语言API,使用起来更加的方便,不需要过多的关心数据库操作的知识。但是它本身也存在一些问题,比如跨平台,因为它是用oc的语言封装的,所以只能在ios开发的时候使用,如果想实现跨平台的操作,来降低开发的成本和维护的成本,就需要使用比较原始的SQLite。

       Core Data是ORM的一种体现,使用Core Data需要用到模型数据的转化,虽然操作简单,不需要直接操作数据库,但是性能没有直接使用SQLite高。但是SQLite使用的时候需要使用c语言中的函数,操作比较麻烦,因此需要对它进行封装。但是如果只是简单地封装,很可能会忽略很多重要的细节,比如如何处理并发以及安全性更问题。

       因此,在这里推荐使用第三方框架FMDB,它是对libsqlite3框架的封装,用起来的步骤与SQLite使用类似,并且它对于多线程的同时操作一个表格时进行了处理,也就意味着它是线程安全的。FMDB是轻量级的框架,使用灵活,它是很多企业开发的首选。
FMDB中重要的类

       FMDatabase:一个FMDatabase对象就代表一个单独的SQLite数据库,用来执行SQL语句

       FMResultSet:使用FMDatabase执行查询后的结果集

       FMDatabaseQueue:用于在多线程中执行多个查询或更新,它是线程安全的

FMDB使用步骤

      1. 下载FMDB文件 fmdb下载地址 ,将FMDB文件夹添加到项目中

      2. 导入sqlite框架,导入FMDatabase.h文件

      3.与SQLite使用步骤类似,需要获取数据库文件路径,然后获得数据库,并打开数据库,然后数据库进行操作,最后关闭数据库。代码如下所示:
 


  1. // ViewController.m
  2. // JRFMDB
  3. //
  4. // Created by jerehedu on 15/6/18.
  5. // Copyright (c) 2015年 jerehedu. All rights reserved.
  6. //
  7.  
  8. #import "ViewController.h"
  9. #import "FMDatabase.h"
  10.  
  11. @interface ViewController () 
  12. @property (nonatomic, strong) FMDatabase *db; 
  13.  
  14. @end
  15.  
  16. @implementation ViewController 
  17.  
  18. - (void)viewDidLoad 
  19. [super viewDidLoad]; 
  20. //导入sqlite框架,导入FMDB文件夹
  21.  
  22. //1.获得数据库文件的路径
  23. NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
  24. NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"]; 
  25. NSLog(@"fileName = %@",fileName); 
  26.  
  27. //2.获得数据库
  28. FMDatabase *db = [FMDatabase databaseWithPath:fileName]; 
  29.  
  30. //3.打开数据库
  31. if ([db open]) { 
  32. NSLog(@"ok"); 
  33.  
  34. //4.创表
  35. BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; 
  36. if (result) { 
  37. NSLog(@"创表成功"); 
  38. }else
  39. NSLog(@"创表失败"); 
  40. self.db=db; 
  41.  
  42. //插入数据
  43. [self insertStu]; 
  44. [self deleteStu:6]; 
  45. [self updateStu:@"apple7_name" :@"7777"]; 
  46.  
  47. [self queryStu]; 
  48. [self dropStu]; 
  49. [self insertStu]; 
  50. [self queryStu]; 
  51.  
  52. //6.关闭数据库
  53. [self.db close]; 
  54.  
  55. #pragma mark 插入数据 
  56. -(void)insertStu 
  57. for (int i=0; i<10; i++) 
  58. NSString *name = [NSString stringWithFormat:@"1apple%i_name",i]; 
  59. int age = arc4random()%3+20
  60.  
  61. //1. executeUpdate : 不确定的参数用?来占位 (后面参数必须都是oc对象)
  62. [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)]; 
  63.  
  64. //2. executeUpdateWithFormat : 不确定的参数用%@、%d等来占位 (参数为原始数据类型)
  65. // [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age];
  66.  
  67. //3. 数组
  68. // [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]];
  69.  
  70.  
  71. #pragma mark 删除数据 
  72. -(void)deleteStu:(int)idNum 
  73. //a. executeUpdate : 不确定的参数用?来占位 (后面参数必须都是oc对象)
  74. // [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)];
  75.  
  76. //b. executeUpdateWithFormat : 不确定的参数用%@、%d等来占位
  77. // [self.db executeUpdateWithFormat:@"delete from t_student where name=%@;",@"apple9_name"];
  78.  
  79. #pragma mark 销毁表格 
  80. -(void)dropStu 
  81. [self.db executeUpdate:@"drop table if exists t_student;"]; 
  82.  
  83. //4.创表
  84. BOOL result=[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"]; 
  85. if (result) { 
  86. NSLog(@"再次创表成功"); 
  87. }else
  88. NSLog(@"再次创表失败"); 
  89.  
  90. #pragma mark 修改数据 
  91. -(void)updateStu:(NSString *)oldName :(NSString*)newName 
  92. // [self.db executeUpdateWithFormat:@"update t_student set name=%@ where name=%@;",newName,oldName];
  93. [self.db executeUpdate:@"update t_student set name=? where name=?",newName,oldName]; 
  94.  
  95. #pragma mark 查询数据 
  96. -(void)queryStu 
  97. //1.执行查询语句
  98. // FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"];
  99. FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)]; 
  100.  
  101. //2.遍历结果集合
  102. while ([resultSet next]) { 
  103. int idNum = [resultSet intForColumn:@"id"]; 
  104. NSString *name = [resultSet objectForColumnName:@"name"]; 
  105. int age = [resultSet intForColumn:@"age"]; 
  106. NSLog(@"id=%i ,name=%@, age=%i",idNum,name,age); 
  107.  
  108.  
  109. - (void)didReceiveMemoryWarning 
  110. [super didReceiveMemoryWarning]; 
  111. // Dispose of any resources that can be recreated.
  112.  
  113. @end    (文/博客园)