iOS FMDB的使用(增,删,改,查,sqlite存取图片)
在上一篇博客我对sqlite的基本使用进行了详细介绍...
但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FMDB的使用... 例: 增,删,改,查,sqlite存取图片有关框架的导入我在上一篇博客进行了详细介绍这里就不在介绍,没有看到上一篇博客的可以点击下面的连接.
接下来我会按照上篇博客的顺序,模式进行介绍.
(增删改查与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
测试结果
图片(插入后查询)
图片(修改后查询)
图片(删除后查询 由于显示无法证实 这里查看数据库)