码迷,mamicode.com
首页 > 移动开发 > 详细

iOSFMDB和CoreData

时间:2016-07-12 18:59:20      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:

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

iOSFMDB和CoreData

标签:

原文地址:http://www.cnblogs.com/pruple/p/5664392.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!