当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS学习笔记(十六)——详解数据库操作(使用FMDB)

iOS学习笔记(十六)——详解数据库操作(使用FMDB)

2019年07月24日  | 移动技术网IT编程  | 我要评论

林庭锋,小冤家简谱,苹果贵族衣柜

ios中原生的sqlite api在使用上相当不友好,在使用时,非常不便。于是,就出现了一系列将sqlite api进行封装的库,例如fmdb、plausibledatabase、sqlitepersistentobjects等,fmdb () 是一款简洁、易用的封装库,这一篇文章简单介绍下fmdb的使用。

在fmdb下载文件后,工程中必须导入如下文件,并使用 libsqlite3.dylib 依赖包。

fmdb同时兼容arc和非arc工程,会自动根据工程配置来调整相关的内存管理代码。

fmdb常用类:

  1. fmdatabase : 一个单一的sqlite数据库,用于执行sql语句。
  2. fmresultset :执行查询一个fmdatabase结果集,这个和android的cursor类似。
  3. fmdatabasequeue :在多个线程来执行查询和更新时会使用这个类。

创建数据库:

db = [fmdatabase databasewithpath:database_path]; 

1、当数据库文件不存在时,fmdb会自己创建一个。

2、 如果你传入的参数是空串:@"" ,则fmdb会在临时文件目录下创建这个数据库,数据库断开连接时,数据库文件被删除。

3、如果你传入的参数是 null,则它会建立一个在内存中的数据库,数据库断开连接时,数据库文件被删除。

打开数据库:

[db open] 

返回bool型。

关闭数据库:

[db close] 

数据库增删改等操作:

除了查询操作,fmdb数据库操作都执行executeupdate方法,这个方法返回bool型。

看一下例子:

创建表:

if ([db open]) { 
    nsstring *sqlcreatetable = [nsstring stringwithformat:@"create table if not exists '%@' ('%@' integer primary key autoincrement, '%@' text, '%@' integer, '%@' text)",tablename,id,name,age,address]; 
    bool res = [db executeupdate:sqlcreatetable]; 
    if (!res) { 
      nslog(@"error when creating db table"); 
    } else { 
      nslog(@"success to creating db table"); 
    } 
    [db close]; 
 
  } 

添加数据:

if ([db open]) { 
    nsstring *insertsql1= [nsstring stringwithformat: 
               @"insert into '%@' ('%@', '%@', '%@') values ('%@', '%@', '%@')", 
               tablename, name, age, address, @"张三", @"13", @"济南"]; 
    bool res = [db executeupdate:insertsql1]; 
    nsstring *insertsql2 = [nsstring stringwithformat: 
                @"insert into '%@' ('%@', '%@', '%@') values ('%@', '%@', '%@')", 
                tablename, name, age, address, @"李四", @"12", @"济南"]; 
    bool res2 = [db executeupdate:insertsql2]; 
     
    if (!res) { 
      nslog(@"error when insert db table"); 
    } else { 
      nslog(@"success to insert db table"); 
    } 
    [db close]; 
 
  } 

修改数据:

if ([db open]) { 
    nsstring *updatesql = [nsstring stringwithformat: 
                @"update '%@' set '%@' = '%@' where '%@' = '%@'", 
                tablename,  age, @"15" ,age, @"13"]; 
    bool res = [db executeupdate:updatesql]; 
    if (!res) { 
      nslog(@"error when update db table"); 
    } else { 
      nslog(@"success to update db table"); 
    } 
    [db close]; 
 
  } 

删除数据:

if ([db open]) { 
     
    nsstring *deletesql = [nsstring stringwithformat: 
                @"delete from %@ where %@ = '%@'", 
                tablename, name, @"张三"]; 
    bool res = [db executeupdate:deletesql]; 
     
    if (!res) { 
      nslog(@"error when delete db table"); 
    } else { 
      nslog(@"success to delete db table"); 
    } 
    [db close]; 
 
  } 

数据库查询操作:

查询操作使用了executequery,并涉及到fmresultset。

if ([db open]) { 
    nsstring * sql = [nsstring stringwithformat: 
             @"select * from %@",tablename]; 
    fmresultset * rs = [db executequery:sql]; 
    while ([rs next]) { 
      int id = [rs intforcolumn:id]; 
      nsstring * name = [rs stringforcolumn:name]; 
      nsstring * age = [rs stringforcolumn:age]; 
      nsstring * address = [rs stringforcolumn:address]; 
      nslog(@"id = %d, name = %@, age = %@ address = %@", id, name, age, address); 
    } 
    [db close]; 
  } 

fmdb的fmresultset提供了多个方法来获取不同类型的数据:

 

数据库多线程操作:

如果应用中使用了多线程操作数据库,那么就需要使用fmdatabasequeue来保证线程安全了。 应用中不可在多个线程中共同使用一个fmdatabase对象操作数据库,这样会引起数据库数据混乱。 为了多线程操作数据库安全,fmdb使用了fmdatabasequeue,使用fmdatabasequeue很简单,首先用一个数据库文件地址来初使化fmdatabasequeue,然后就可以将一个闭包(block)传入indatabase方法中。 在闭包中操作数据库,而不直接参与fmdatabase的管理。

fmdatabasequeue * queue = [fmdatabasequeue databasequeuewithpath:database_path]; 
  dispatch_queue_t q1 = dispatch_queue_create("queue1", null); 
  dispatch_queue_t q2 = dispatch_queue_create("queue2", null); 
   
  dispatch_async(q1, ^{ 
    for (int i = 0; i < 50; ++i) { 
      [queue indatabase:^(fmdatabase *db2) { 
         
        nsstring *insertsql1= [nsstring stringwithformat: 
                   @"insert into '%@' ('%@', '%@', '%@') values (?, ?, ?)", 
                   tablename, name, age, address]; 
         
        nsstring * name = [nsstring stringwithformat:@"jack %d", i]; 
        nsstring * age = [nsstring stringwithformat:@"%d", 10+i]; 
         
         
        bool res = [db2 executeupdate:insertsql1, name, age,@"济南"]; 
        if (!res) { 
          nslog(@"error to inster data: %@", name); 
        } else { 
          nslog(@"succ to inster data: %@", name); 
        } 
      }]; 
    } 
  }); 
   
  dispatch_async(q2, ^{ 
    for (int i = 0; i < 50; ++i) { 
      [queue indatabase:^(fmdatabase *db2) { 
        nsstring *insertsql2= [nsstring stringwithformat: 
                   @"insert into '%@' ('%@', '%@', '%@') values (?, ?, ?)", 
                   tablename, name, age, address]; 
         
        nsstring * name = [nsstring stringwithformat:@"lilei %d", i]; 
        nsstring * age = [nsstring stringwithformat:@"%d", 10+i]; 
         
        bool res = [db2 executeupdate:insertsql2, name, age,@"北京"]; 
        if (!res) { 
          nslog(@"error to inster data: %@", name); 
        } else { 
          nslog(@"succ to inster data: %@", name); 
        } 
      }]; 
    } 
  }); 

源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网