建立一个单例:

DataBaseHandle.h

#import <Foundation/Foundation.h>
@class PersonModel;
@class FMDatabase;
@interface DataBaseHandle : NSObject
@property(nonatomic,retain)FMDatabase *db;
//创建单例的的接口
+ (DataBaseHandle *)shareDateBaseHandle;
//创建一个Person表格
- (void)creatPersonTable;
//插入person的方法
- (void)insertPersonTable : (PersonModel *)person;
//写一个删除人的接口
- (void)deletePersonByPerssonID : (NSString *)ID;
//写一个修改人的接口
- (void)uodatePerson : (NSString *)age ByPersonID : (NSString *)ID;
//写一个查询所有人的接口
- (NSMutableArray *)selectAllPersonFromPersonTable;
@end

DataBaseHandle.m

#import "DataBaseHandle.h"
#import "FMDB.h"
#import "PersonModel.h"
@implementation DataBaseHandle
- (void)dealloc
{
    self.db = nil;
    [super dealloc];
}

创建单例的的接口:

//创建单例对象使其存在于静态区
static DataBaseHandle *handle = nil;
//创建单例的的借口
+ (DataBaseHandle *)shareDateBaseHandle{

    @synchronized(self){
        if (handle == nil) {
            handle = [[DataBaseHandle alloc]init];

        }
    }
    return handle;
}

写一个私有的方法,返回数据库的路径

- (NSString *)dbpath{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"db.sqlite"];

}

创建一个Person表格

//创建一个Person表格
- (void)creatPersonTable{
   //初始化数据库对象
    self.db = [FMDatabase databaseWithPath: [self dbpath]];
    //打开数据库
   BOOL isOpen = [self.db open];
    if (isOpen) {
        NSLog(@"打开成功");
        //创建表
     BOOL isCreat =   [self.db executeUpdate:@"create table if not exists Person(id integer primary key autoincrement,name text,gender text,age integer,salary integer)"];
        NSLog(@"%@",isCreat ? @"创建成功":@"创建失败");

    }else{
        NSLog(@"打开失败");
    }
}

四种方法:增、删、改、查;

//插入person的方法
- (void)insertPersonTable : (PersonModel *)person{
  BOOL isInsert =  [self.db executeUpdate:@"insert into Person(name,age)values(?,?)",person.name,person.age];
    NSLog(@"%@",isInsert ? @"插入成功":@"插入失败");

}

//写一个删除的接口
- (void)deletePersonByPerssonID : (NSString *)ID{
   BOOL isDelete = [self.db executeUpdate:@"delete from Person where id = ?",ID];
    NSLog(@"%@",isDelete ? @"删除成功":@"删除失败");
}

//写一个修改人的接口
- (void)uodatePerson : (NSString *)age ByPersonID : (NSString *)ID{
   BOOL isUpdate = [self.db executeUpdate:@"update Person set age = ? where id = ?",age,ID];
    NSLog(@"%@",isUpdate ? @"修改成功":@"修改失败");
}

//写一个查询所有人的接口
- (NSMutableArray *)selectAllPersonFromPersonTable{
    FMResultSet *set = [self.db executeQuery:@"select * from Person"];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    while ([set next]) {
        NSInteger ID = [set intForColumn:@"id"];
        NSString *name = [set stringForColumn:@"name"];
        NSInteger age = [set intForColumn:@"age"];
        //创建Person对象存储信息
        PersonModel *p = [[PersonModel alloc]init];
        p.ID = [NSString stringWithFormat:@"%ld",ID];
        p.name = name;
        p.age = [NSString stringWithFormat:@"%ld",age];
        //添加到数组
        [array addObject:p];
        [p release];
    }
    return array;
}

建一个model类

PersonModel.h
#import <Foundation/Foundation.h>
@interface PersonModel : NSObject
@property(nonatomic,copy)NSString *ID;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *age;
@end

PersonModel.m
#import "PersonModel.h"

@implementation PersonModel
- (void)dealloc
{
    self.name = nil;
    self.age = nil;
    self.ID = nil;
    [super dealloc];
}
@end

===============================测试调用===============================

#import "FirstViewController.h"
#import "DataBaseHandle.h"
#import "PersonModel.h"
@interface FirstViewController ()
@property(nonatomic,retain)NSMutableArray *dataSource;//接收查询的结果
@end

@implementation FirstViewController
- (void)dealloc
{
    self.dataSource = nil;
    [super dealloc];
}
//懒加载
- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
    }

    return [[_dataSource retain]autorelease];
}

TEXT:

- (void)viewDidLoad {
    [super viewDidLoad];
    //调用并验证
    [[DataBaseHandle shareDateBaseHandle]creatPersonTable];
    NSLog(@"%@",NSHomeDirectory());
    PersonModel *p = [[PersonModel alloc]init];
    p.name = @"小韩哥";
    p.age = @"20";
    //调用插入person的方法
//    [[DataBaseHandle shareDateBaseHandle]insertPersonTable:p];

    //接收数据库返回的查询结果
    self.dataSource = [[DataBaseHandle shareDateBaseHandle]selectAllPersonFromPersonTable];

    //调用删除人的方法
    [[DataBaseHandle shareDateBaseHandle]deletePersonByPerssonID:@"9"];

}

配置显示:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"firstcell" forIndexPath:indexPath];
    PersonModel *p = self.dataSource[indexPath.row];
    cell.textLabel.text = p.name;

    return cell;
}

布局预览:

提示:重在封装SQLite思想,不在效果,能有效调用即可!

iOS中 用FMDB封装一个SQLite数据库的更多相关文章

  1. 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密

    在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...

  2. iOS中使用FMDB事务批量更新数据库

    今天比较闲看到大家在群里讨论关于数据库操作的问题,其中谈到了"事务"这个词,坦白讲虽然作为计算机专业的学生,在上学的时候确实知道存储过程.触发器.事务等等这些名词的概念,但是由于毕 ...

  3. IOS开发-UI学习-sqlite数据库的操作

    IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...

  4. 在VB中利用Nuget包使用SQLite数据库和Linq to SQLite

    上午解决了在C#中利用Nuget包使用SQLite数据库和Linq to SQLite,但是最后生成的是C#的cs类文件,对于我这熟悉VB而对C#白痴的来说怎么能行呢? 于是下午接着研究,既然生成的是 ...

  5. 创建一个 SQLite 数据库

    首先,我们学习如何创建一个SQLite 数据库.如果想要在data/example.sqlite 这个路径中创建一个示例数据库,就必须确保该路径存在.如果该路径不存在,就必须先创建路径:if (!di ...

  6. 将 flask 中的 session 存储到 SQLite 数据库中

    将 flask 中的 session 存储到 SQLite 数据库中 使用 flask 构建服务器后端时,常需要在浏览器端存储 cookie 用于识别不同用户,根据不同的 cookie 判断出当前请求 ...

  7. Electron中使用sql.js操作SQLite数据库

    推荐sql.js——一款纯js的sqlite工具. 一.关于sql.js sql.js(https://github.com/kripken/sql.js)通过使用Emscripten编译SQLite ...

  8. android 一个SQLite数据库多个数据表的基本使用框架 (带demo)

    android 一个SQLite数据库多个数据表(带demo) 前言        demo演示        一.搭建        二.建立实体类        三.建立数据库操作类        ...

  9. 在ios中使用FMDB

    SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyli ...

随机推荐

  1. KMP及其改进算法

    本文主要讲述KMP已经KMP的一种改进方法.若发现不正确的地方,欢迎交流指出,谢谢! KMP算法的基本思想: KMP的算法流程: 每当一趟匹配过程中出现字符比较不等时,不需回溯 i 指针,而是利用已经 ...

  2. 算法二叉搜索树之AVL树

    最近学习了二叉搜索树中的AVL树,特在此写一篇博客小结. 1.引言 对于二叉搜索树而言,其插入查找删除等性能直接和树的高度有关,因此我们发明了平衡二叉搜索树.在计算机科学中,AVL树是最先发明的自平衡 ...

  3. Java 并发编程——Executor框架和线程池原理

    Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...

  4. Logistic Regression 算法向量化实现及心得

    Author: 相忠良(Zhong-Liang Xiang) Email: ugoood@163.com Date: Sep. 23st, 2017 根据 Andrew Ng 老师的深度学习课程课后作 ...

  5. CSS 常用的命名规则

    (1)页面结构 容器: container 页头:header 内容:content/container 页面主体:main 页尾:footer 导航:nav 侧栏:sidebar 栏目:column ...

  6. 剑指架构师系列-Redis安装与使用

    1.安装Redis 我们在VMware中安装CentOS 64位系统后,在用户目录下下载安装Redis. 下载redis目前最稳定版本也是功能最完善,集群支持最好并加入了sentinel(哨兵-高可用 ...

  7. 剑指架构师系列-持续集成之Maven实现项目的编译、发布和部署

    Maven组织项目进行编译.部署 Maven项目基本的结构说明如下: mazhi  // 控制所有荐的编译.部署.发布 mazhi-app-parent  // 项目的父项目,有一些公共的设置可以被子 ...

  8. Docker其它安全特性

    除了能力机制之外,还可以利用一些现有的安全机制来增强使用 Docker 的安全性,例如 TOMOYO, AppArmor, SELinux, GRSEC 等. Docker 当前默认只启用了能力机制. ...

  9. 部署 Helm - 每天5分钟玩转 Docker 容器技术(162)

    本节我们将安装和部署 Helm 客户端和 Tiller 服务器. Helm 客户端 通常,我们将 Helm 客户端安装在能够执行 kubectl 命令的节点上,只需要下面一条命令: curl http ...

  10. 操作系统内核Hack:(一)实验环境搭建

    操作系统内核Hack:(一)实验环境搭建 三四年前,心血来潮,入手<Orange's:一个操作系统的实现>学习操作系统内核,还配套买了王爽的<汇编语言(第二版)>和<80 ...