建立一个单例:

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. 重构:从Promise到Async/Await

    摘要: 夸张点说,技术的发展与历史一样,顺之者昌,逆之者亡.JS开发者们,赶紧拥抱Async/Await吧! GitHub仓库: Fundebug/promise-asyncawait 早在半年多之前 ...

  2. 移动端meta标签缓存设置

    1.<meta charset="utf-8"> 2.<meta content="width=device-width, initial-scale= ...

  3. EXISTS的使用详解

    .exists的使用场合: exists 用于只能用于子查询,可以替代in,若匹配到结果,则退出内部 查询,并将条件标志为true,传回全部结果资料,in 不管匹配到匹配不到都 全部匹配完毕,使用ex ...

  4. java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB

    错误现象: [framework] 2016-05-26 11:34:53,590 -INFO  [http-bio-8080-exec-7] -1231863 -com.dhcc.base.db.D ...

  5. python脚本文件传参并通过token登录后爬取数据实例

    from bs4 import BeautifulSoup import requests import sys class Zabbix(object): def __init__(self, he ...

  6. 非参数估计:核密度估计KDE

    http://blog.csdn.net/pipisorry/article/details/53635895 核密度估计Kernel Density Estimation(KDE)概述 密度估计的问 ...

  7. 最优秀的网络框架retrofit

    由于某学员要求所以我打算写一篇 标题先记录下来 我会在一周内完成此篇文章

  8. Ubuntu使用dpkg安装软件依赖问题解决 ubuntu-tweak ubuntu 16.04 LTS 系统清理

    Ubuntu使用dpkg安装软件依赖问题解决 这里以在ubuntu 16.04安装Ubuntu Tweak为例进行说明,通常安装包依赖问题都可以用这种方法解决: sudo apt-get instal ...

  9. Android逆向工程

    在Root前提下,我们可以使用Hooker方式绑定so库,通过逆向方式篡改数值,从而达到所谓破解目的.然而,目前无论是软件加固方式,或是数据处理能力后台化,还是客户端数据真实性验证,都有了一定积累和发 ...

  10. Protobuf-net判断字段是否有值

    Protobuf-net判断字段是否有值Unity3d使用Protobuf-net序列化数据与服务器通信,但是发现默认情况下,Protobuf-net生成的cs文件中没有接口判断可选参数是否有值.需有 ...