首页 > 学院 > 开发设计 > 正文

IOS数据存储之FMDB详解

2019-11-14 18:45:01
字体:
来源:转载
供稿:网友

  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使用步骤类似,需要获取数据库文件路径,然后获得数据库,并打开数据库,然后数据库进行操作,最后关闭数据库。代码如下所示:

//  ViewController.m//  JRFMDB////  Created by jerehedu on 15/6/18.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#import "ViewController.h"#import "FMDatabase.h"@interface ViewController ()@PRoperty (nonatomic, strong) FMDatabase *db;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    //导入sqlite框架,导入FMDB文件夹        //1.获得数据库文件的路径    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    NSString *fileName=[doc stringByAppendingPathComponent:@"student.sqlite"];    NSLog(@"fileName = %@",fileName);        //2.获得数据库    FMDatabase *db = [FMDatabase databaseWithPath:fileName];        //3.打开数据库    if ([db open]) {        NSLog(@"ok");                //4.创表        BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];        if (result) {            NSLog(@"创表成功");        }else{            NSLog(@"创表失败");        }    }    self.db=db;        //插入数据    [self insertStu];    [self deleteStu:6];    [self updateStu:@"apple7_name" :@"7777"];        [self queryStu];    [self dropStu];    [self insertStu];    [self queryStu];        //6.关闭数据库    [self.db close];}#pragma mark 插入数据-(void)insertStu{    for (int i=0; i<10; i++)    {        NSString *name = [NSString stringWithFormat:@"1apple%i_name",i];        int age = arc4random()%3+20;                //1.  executeUpdate : 不确定的参数用?来占位 (后面参数必须都是oc对象)        [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);",name,@(age)];                //2.  executeUpdateWithFormat : 不确定的参数用%@、%d等来占位 (参数为原始数据类型)        //        [self.db executeUpdateWithFormat:@"insert into t_student (name, age) values (%@, %i);",name,age];                //3. 数组        //        [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?,?);" withArgumentsInArray:@[name,@(age)]];    }    }#pragma mark 删除数据-(void)deleteStu:(int)idNum{    //a.  executeUpdate : 不确定的参数用?来占位 (后面参数必须都是oc对象)    //    [self.db executeUpdate:@"delete from t_student where id=?;",@(idNum)];        //b.  executeUpdateWithFormat : 不确定的参数用%@、%d等来占位    //    [self.db executeUpdateWithFormat:@"delete from t_student where name=%@;",@"apple9_name"];}#pragma mark 销毁表格-(void)dropStu{    [self.db executeUpdate:@"drop table if exists t_student;"];        //4.创表    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);"];    if (result) {        NSLog(@"再次创表成功");    }else{        NSLog(@"再次创表失败");    }}#pragma mark 修改数据-(void)updateStu:(NSString *)oldName :(NSString*)newName{    //    [self.db executeUpdateWithFormat:@"update t_student set name=%@ where name=%@;",newName,oldName];    [self.db executeUpdate:@"update t_student set name=? where name=?",newName,oldName];}#pragma mark 查询数据-(void)queryStu{    //1.执行查询语句    //    FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student;"];    FMResultSet *resultSet = [self.db executeQuery:@"select * from t_student where id<?;",@(14)];        //2.遍历结果集合    while ([resultSet next]) {        int idNum = [resultSet intForColumn:@"id"];        NSString *name = [resultSet objectForColumnName:@"name"];        int age = [resultSet intForColumn:@"age"];        NSLog(@"id=%i ,name=%@, age=%i",idNum,name,age);    }    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

  疑问咨询或技术交流,请加入官方QQ群:JRedu技术交流 (452379712)

 

作者:杰瑞教育
出处:http://www.VEVb.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表