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

iOS FMDB的以(增,删,改,查,sqlite存取图片)


当高达一致篇博客我本着sqlite的为主用进行了详细介绍…
然在骨子里支出中原生使用的效率是十分少的…
即篇博客我以见面比全面的牵线FMDB的用…
条例: 增,删,改,查,sqlite存取图片

至于框架的导入我以直达一样篇博客进行了详尽介绍这里虽非以介绍,没有看上亦然篇博客的得点击下面的连接.

iOS sqlite3 的中心采用(增 删 改
查)

对接下我会以上篇博客的逐一,模式开展介绍.
(增删改查与sqlite存取图片我会通过简单个工程介绍)

创立数据库与申

代码:

@interface ViewController ()

@property (nonnull, strong) FMDatabase * database;

@end

- (void)viewDidLoad {

    [super viewDidLoad];
    //数据库在沙盒中的路径
    NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
    NSLog(@"%@",fileName);

    //创建数据库
    self.database = [[FMDatabase alloc]initWithPath:fileName];

    //打开数据库
    if ([self.database open]) {
        NSLog(@"打开数据库成功");
        //创建表 返回值为BOOL
        BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,name text)"];
        if (flag) {
            NSLog(@"成功建表");
        }else{
            NSLog(@"失败建表");
        }

    //关闭数据库
        [self.database close];

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

}

图片(创建数据库暨申)

代码:

- (IBAction)insert:(id)sender {

    if ([self.database open]) {
        for (NSInteger i = 0; i < 100; i ++) {

            BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (name) values(?)",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99)]];

            if (flag) {
                NSLog(@"插入成功");
            }else{
                NSLog(@"插入失败");
            }

        }
    }
    [self.database close];
}

图片(增)

代码:

- (IBAction)delete:(id)sender {

    if ([self.database open]) {
        for (NSInteger i = 0; i < 100; i ++) {

            BOOL flag = [self.database executeUpdate:@"delete from t_testOfFMDB where id < 50"];

            if (flag) {
                NSLog(@"删除成功");
            }else{
                NSLog(@"删除失败");
            }

        }
    }
    [self.database close];

}

图片(删)

代码:

- (IBAction)update:(id)sender {

    if ([self.database open]) {
        for (NSInteger i = 0; i < 100; i ++) {

            BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];

            if (flag) {
                NSLog(@"修改成功");
            }else{
                NSLog(@"修改失败");
            }

        }
    }
    [self.database close];
}

图片(改)

代码:

- (IBAction)select:(id)sender {

    if ([self.database open]) {

        //返回查询数据的结果集
        FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
        //查询表中的每一个记录
        while ([resultSet next]) {

            NSString * name = [resultSet stringForColumn:@"name"];
            NSLog(@"%@",name);

        }

    }
    [self.database close];
}

图片(查)

整代码

//
//  ViewController.m
//  FMDB的使用
//
//  Created by ma c on 16/5/10.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"
#import "FMDB.h"

@interface ViewController ()

@property (nonnull, strong) FMDatabase * database;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    //数据库在沙盒中的路径
    NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
    NSLog(@"%@",fileName);

    //创建数据库
    self.database = [[FMDatabase alloc]initWithPath:fileName];

    //打开数据库
    if ([self.database open]) {
        NSLog(@"打开数据库成功");
        //创建表 返回值为BOOL
        BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,name text)"];
        if (flag) {
            NSLog(@"成功建表");
        }else{
            NSLog(@"失败建表");
        }

    //关闭数据库
        [self.database close];

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

}

- (IBAction)insert:(id)sender {

    if ([self.database open]) {
        for (NSInteger i = 0; i < 100; i ++) {

            BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (name) values(?)",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99)]];

            if (flag) {
                NSLog(@"插入成功");
            }else{
                NSLog(@"插入失败");
            }

        }
    }
    [self.database close];
}

- (IBAction)update:(id)sender {

    if ([self.database open]) {
        for (NSInteger i = 0; i < 100; i ++) {

            BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];

            if (flag) {
                NSLog(@"修改成功");
            }else{
                NSLog(@"修改失败");
            }

        }
    }
    [self.database close];
}

- (IBAction)update:(id)sender {

    if ([self.database open]) {
        for (NSInteger i = 0; i < 100; i ++) {

            BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set name = 'hello world' where id > 50"];

            if (flag) {
                NSLog(@"修改成功");
            }else{
                NSLog(@"修改失败");
            }

        }
    }
    [self.database close];
}

- (IBAction)select:(id)sender {

    if ([self.database open]) {

        //返回查询数据的结果集
        FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
        //查询表中的每一个记录
        while ([resultSet next]) {

            NSString * name = [resultSet stringForColumn:@"name"];
            NSLog(@"%@",name);

        }

    }
    [self.database close];
}

@end

sqlite存取图片

此地发出几碰注意:

  • 图片无法直接存入数据库中得把图片转换为二进制后存入数据库
  • 仲进制存储的格式为 blob

完代码

//
//  ViewController.m
//  FMDB的使用
//
//  Created by ma c on 16/5/10.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"
#import "FMDB.h"

@interface ViewController ()

@property (nonnull, strong) FMDatabase * database;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    //数据库在沙盒中的路径
    NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"testOfFMDB.sqlite"];
    NSLog(@"%@",fileName);

    //创建数据库
    self.database = [[FMDatabase alloc]initWithPath:fileName];

    //打开数据库
    if ([self.database open]) {
        NSLog(@"打开数据库成功");
        //创建表 返回值为BOOL
        BOOL flag = [self.database executeUpdate:@"create table if not exists t_testOfFMDB (id integer primary key autoincrement,image blob)"];
        if (flag) {
            NSLog(@"成功建表");
        }else{
            NSLog(@"失败建表");
        }

    //关闭数据库
        [self.database close];

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

}
- (IBAction)insert:(id)sender {

    if ([self.database open]) {

        NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@"BG.jpg"], 1);

            BOOL flag = [self.database executeUpdate:@"insert into t_testOfFMDB (image) values(?)",data];

            if (flag) {
                NSLog(@"插入成功");
            }else{
                NSLog(@"插入失败");
            }


    }
    [self.database close];
}
- (IBAction)delete:(id)sender {

    if ([self.database open]) {

            BOOL flag = [self.database executeUpdate:@"delete from t_testOfFMDB"];

            if (flag) {
                NSLog(@"删除成功");
            }else{
                NSLog(@"删除失败");
            }

        }
    [self.database close];

}
- (IBAction)update:(id)sender {

    if ([self.database open]) {

            NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@"CX.jpg"], 1);

            BOOL flag = [self.database executeUpdate:@"update t_testOfFMDB set image = ?",data];

            if (flag) {
                NSLog(@"修改成功");
            }else{
                NSLog(@"修改失败");
            }

        }
    [self.database close];
}
- (IBAction)select:(id)sender {

    if ([self.database open]) {

        //返回查询数据的结果集
        FMResultSet * resultSet = [self.database executeQuery:@"select * from t_testOfFMDB"];
        //查询表中的每一个记录
        while ([resultSet next]) {


            NSData * data = [resultSet dataForColumn:@"image"];

            UIImage * image = [UIImage imageWithData:data];

            self.imageView.image = image;

        }

    }
    [self.database close];
}

@end

测试结果

图形(插入后查询)sqlite

图表(修改后查询)

图(删除后查询 由于显得无法说明 这里翻数据库)

网站地图xml地图