简介:

SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库。iOS SDK 很早就支持了 SQLite,在使用时,只需要加入 libsqlite3.dylib 依赖以及引入 sqlite3.h 头文件即可。但是,原生的 SQLite API 在使用上相当不友好,在使用时,非常不便。于是,开源社区中就出现了一系列将 SQLite API 进行封装的库,而 FMDB (https://github.com/ccgus/fmdb) 则是开源社区中的优秀者。

使用: (BESTAccountsManager 为账号数据管理分类, BESTAccountItem 为账号模型)

static FMDatabase *_db;
static BESTAccountsManager *sharedManager = nil;
// 单例
+ (instancetype)sharedManager {
    @synchronized (self) {
        if (sharedManager == nil) {
            sharedManager = [[BESTAccountsManager alloc] init];
        }
    }
    return sharedManager;
}

// 打开数据库

- (void)openAccountManager {
    // 判断 caches 文件夹是否存在.不存在则创建
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL tag = [manager fileExistsAtPath:path isDirectory:NULL];
    if (!tag) {
        [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    NSString *pathString = [path stringByAppendingPathComponent:@"AccountManager.db"];
    NSLog(@"account_path - %@", pathString);
    _db = [FMDatabase databaseWithPath:pathString];
   
    if ([_db open]) {
        BOOL res = [[BESTAccountsManager sharedManager] createTable];
        
        if (!res) {
            NSLog(@"create table Accounts failed!");
        }
    }
}

// 创建数据库
- (BOOL)createTable {
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'Account'('id' PRIMARY KEY AUTOINCREMENT NOT NULL, 'UserName' TEXT NOT NULL, 'Password' TEXT NOT NULL, 'Host' TEXT NOT NULL)"];
    
    return [_db executeUpdate:sql];
}

// 增
- (void)insertAccountItem:(BESTAccountItem *)item {
    if ([_db open]) {
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO 'Account' ('UserName', 'Password', 'Host') VALUES ('%@', '%@', '%@')", item.userName, item.password, item.host];
        BOOL res = [_db executeUpdate:sql];
        if (res) {
            NSLog(@"insert account item succeed!");
        } else {
            NSLog(@"insert account item failed!");
        }
    }
    [_db close];
}

// 删
- (void)deleteAccountItem:(BESTAccountItem *)item {
    if ([_db open]) {
        NSString *sql = [NSString stringWithFormat:@"DELETE FROM 'Account' WHERE 'UserName' = '%@' AND 'Host' = '%@'", item.userName, item.host];
        BOOL res = [_db executeUpdate:sql];
        if (res) {
            NSLog(@"delete account item succeed!");
        } else {
            NSLog(@"delete account item failed!");
        }
    }
    [_db close];
}

// 改
- (void)updataAccountItem:(BESTAccountItem *)item {
    if ([_db open]) {
        NSString *sql = [NSString stringWithFormat:@"UPDATE 'Account' SET 'Password' = '%@' WHERE 'UserName' = '%@' AND 'Host' = '%@'", item.password, item.userName, item.host];
        BOOL res = [_db executeUpdate:sql];
        if (res) {
            NSLog(@"update account item succeed!");
        } else {
            NSLog(@"update account item failed!");
        }
    }
    [_db close];
}

// 查
- (NSArray *)queryAccountItems {
    NSMutableArray *accounts = [NSMutableArray array];
    if ([_db open]) {
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM 'Account'"];
        FMResultSet *set = [_db executeQuery:sql];
        while ([set next]) {
            BESTAccountItem *item = [[BESTAccountItem alloc] init];
            item.userName = [set stringForColumn:@"UserName"];
            item.password = [set stringForColumn:@"Password"];
            item.host = [set stringForColumn:@"Host"];
            [accounts addObject:item];
        }
    }
    [_db close];
    
    return accounts;
}

iOS FMDB的使用的更多相关文章

  1. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  2. IOS FMDB 获取数据库表和表中的数据

    ios开发中,经常会用到数据库sqlite的知识,除了增,删,改,查之外,我们说说如何获取数据库中有多少表和表相关的内容. 前言 跟数据库使用相关的一般的增删改查的语句,这里就不做解释了.在网上有很多 ...

  3. iOS FMDB 不需要关闭

    以前做了一个应用,里面用到了FMDB,进行每一次操作前,都open,完成操作后都close.因为我是参考他们以前的代码.程序初期没发现什么问题,程序完成后,各种卡顿就出现了!即使我是放在新线程里操作的 ...

  4. iOS FMDB

    FMDB FMDB概述 什么是FMDB * FMDB是iOS平台的SQLite数据库框架 * FMDB以OC的方式封装了SQLite的C语言API FMDB的优点 * 使用起来更加面向对象,省去了很多 ...

  5. iOS | FMDB快速上手

    任何的开发都或多或少的接触到数据库,而在IOS中一般使用的是SQLite数据库,这是一个轻量功能较为不错的数据库.而现在用到比较多的第三方数据库操作框架就是FMDB.废话不多说,相信查找到这篇文章的都 ...

  6. iOS FMDB小试了一下

    今天从早上9点,一直在看FMDB,知道中午11:40.我的效率是不是很低下.中间也碰到了几个小bug. 虽然做了一个小demo,但是觉得还比不上在项目中使用中锻炼的多,先暂且一总结. 先下载FMDB的 ...

  7. iOS FMDB官方使用文档 G-C-D的使用 提高性能(翻译)(转)

    由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此同时,把SQLite的文档页 http://www.sqlite.org/docs.html 加到你的书签中.自动 ...

  8. IOS FMDB模糊查询

    http://blog.sina.com.cn/s/blog_9630f1310101fx1d.html /查询记录 -(NSArray*)selectitemDream_desc:(JiemengS ...

  9. iOS FMDB的是使用和注意事项

    1.FMDB 默认的使用方法不是线程安全的. 2.Sqlite 默认不支持外键. 3.Sqlite 不支持用 ALTER 关键字给已有表添加外键约束 解决: 1.FMDBDatabaseQueue 2 ...

随机推荐

  1. JS之对象

    每个对象的属性有两种,每种属性有4中特征描述符 1.数据属性 1.1 [[configurable]]:表示不能通过delete删除属性,不能修改属性的特性,不能将数据属性改为访问器属性,默认值fal ...

  2. JSTL定制标签 - 递归标签显示属性结构

  3. Windows server 2008 R2搭建主域控制器 + 辅域控制器

    一:实验模拟环境: Zhuyu公司是一个小公司,随着公司状大,公司越来越重视信息化建设,公司考虑到计算机用户权限集中管理及共享资源同步管理, 需要架设一套AD域控服务器,考虑到成本和日后管理问题,计划 ...

  4. 计划将项目中使用entity framework的要点记录到改栏目下

    ef监控sql执行性能日志.http://www.cnblogs.com/CreateMyself/p/5277681.html http://123.122.205.38/cn_sql_server ...

  5. eclipse中配置tomcat

    配置eclipse中的tomcat非常简单,之前以为windows下和mac中可能会不一样,但是经过配置发现是一样的: 下面就是在eclipse中简单的配置tomcat如下(mac和windows中都 ...

  6. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  7. meta标签详解(meta标签的作用)///////////////////////////转

    meta标签详解(meta标签的作用) 很多人却忽视了HTML标签META的强大功效,一个好的META标签设计可以大大提高你的个人网站被搜索到的可能性,有兴趣吗,谁我来重新认识一下META标签吧   ...

  8. 执行gem install linne时报错

    由于linner安装实际上是从 rubygems.org 获得的,而其被墙,所以,需要寻找国内的镜像进行安装: 第一种方法: gem sources --remove https://rubygems ...

  9. js setInterval

    var monitorInterval = null;    //检索cs 是否处理完成 开始: monitorInterval = setInterval(function () { CheckCS ...

  10. MyEclipse基础配置

    1.设置默认工作空间编码 window/preferences/general/workspace/Text file encoding 2.设置文件默认打开方式 xml建议设置 html建议设置 j ...