iOSFMDB和CoreData

时间:2023-03-09 18:09:13
iOSFMDB和CoreData

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