首页 > 技术文章 > iOSFMDB和CoreData

pruple 2016-07-12 17:53 原文

转发:http://wenku.baidu.com/link?url=LSPSZSPxN4pVwWNwqEXSoY0-jlnXq-_14C7qV1FV9_gFIMPjdKlXrG4Nrh_08EZS10IcYm3syaulljwJ0djvOjePa8Hl9Rq1GvZv50yz7bi

 

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. // 

  1. 7.  

8. #import "ViewController.h" 

9. #import "FMDatabase.h" 

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

推荐阅读