建立一个单例:

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. static class 静态类(Java)

    一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...

  2. 深入理解final关键字

    在了解了final关键字的基本用法之后,这一节我们来看一下final关键字容易混淆的地方. 1.类的final变量和普通变量有什么区别? 当用final作用于类的成员变量时,成员变量(注意是类的成员变 ...

  3. WMI远程启动软件(某个应用程序)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...

  4. 3-学习GPRS_Air202(需要知道的关于Lua的一些基本的知识)

      http://www.cnblogs.com/yangfengwu/p/8948935.html 学东西一定是打破沙锅学到底,有问题就解决问题,不要试图去回避或者放弃解决当前的问题,如果总是回避或 ...

  5. iOS 10 适配 ATS

    一. HTTPS 其实HTTPS从最终的数据解析的角度,与HTTP没有任何的区别,HTTPS就是将HTTP协议数据包放到SSL/TSL层加密后,在TCP/IP层组成IP数据报去传输,以此保证传输数据的 ...

  6. uboot-tiny4412启动流程(下)----如何将自己的裸板测试程序加入uboot中启动测试

    今天在工作上搞了一天高通的芯片uboot程序,目的是希望将一个裸板的程序移植到uboot中,并且开机让它运行.这个芯片是NXP4330,目前是高通的一个芯片,基于ARM-contexA9架构,那么就跟 ...

  7. J2EE进阶(十九)FileNotFoundException: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd

    J2EE进阶(十九)Nested exception: java.io.FileNotFoundException: http://hibernate.org/dtd/hibernate-mappin ...

  8. 从Cell类型转变成数据型

    我们有一个如下的cell数据 cdata = {'1' '11' '111' '1111' '11111'}; 现在要把他转变成double型的数组,很自然会想到的方法是cell2mat,可悲的是会遇 ...

  9. bash shell while语法

    在编写脚本时,一定要注意空格 基本语法: while [ condition ] do command1 command2 command3 done condition为true时命令1到命令3将会 ...

  10. Hadoop与分布式数据处理 Spark VS Hadoop有哪些异同点?

    Spark是一个开源的通用并行分布式计算框架,由加州大学伯克利分校的AMP实验室开发,支持内存计算.多迭代批量处理.即席查询.流处理和图计算等多种范式.Spark内存计算框架适合各种迭代算法和交互式数 ...