iOS 数据储存--SQLite 操作数据库-FMDB,sqlite数据类型,保存图片,demo
1、SQLite 语句中 数据类型的储存
/* 不区分大小写
char(长度)。字符串
NULL. 空值
INTEGER. 整型
REAL.浮点型
TEXT.文本类型
BLOB. 二进制类型,用来存储文件,比如图片。
2、通过单利 操作数据库保存 :Id Name UIImage .h+.m
#import <Foundation/Foundation.h> @interface SaveDateToLocal : NSObject +(SaveDateToLocal*)SaveDateToLocalShare; /** 添加数据 @param Id id @param Image uiimage @param Name string */ -(void)SaveDateAddDateWithId:(NSString*)Id ImageviewImage:(UIImage*)Image nickName:(NSString*)Name; /** 修改数据 @param Id id @param Name name @param Image uiimage */ -(void)SaveDatechangeMessageWithId:(NSString*)Id Name:(NSString*)Name Image:(UIImage *)Image; /** 查找数据 @param Id id @return 包含数据的字典 */ -(NSDictionary*)SaveDatesearchMessageWithId:(NSString*)Id; /** 删除数据 @param Id id */ -(void)SaveDatedeleteMessageWithId:(NSString*)Id; @end
.m
将图片转化成base64编码格式的字符串,直接以字符串的形式存放入数据库 存取方法不做过多介绍,上篇已经做过介绍,主要展示以下转化过程: [html] view plain copy //图片转化为base64字符串 UIImage *originImage = [UIImage imageNamed:@"origin.png"]; NSData *data = UIImageJPEGRepresentation(originImage, 1.0f); NSString *encodedImageStr =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];; NSLog(@"Encoded image:%@", encodedImageStr); [html] view plain copy //base64字符串转化为图片 NSData *decodedImageData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding]; UIImage *decodedImage = [UIImage imageWithData:decodedImageData]; NSLog(@"Decoded image size: %@", NSStringFromCGSize(decodedImage.size)); */ #import "SaveDateToLocal.h" #import <FMDatabase.h> @implementation SaveDateToLocal static SaveDateToLocal * saveDateToLocal =nil; static FMDatabase * dataBase = nil;//静态变量全局使用 +(SaveDateToLocal*)SaveDateToLocalShare{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ saveDateToLocal =[[self alloc]init]; NSString * strpath =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/userchat.db"]; //创建数据库 dataBase = [FMDatabase databaseWithPath:strpath]; //打开数据库 BOOL ret = [dataBase open]; if (ret == NO) { NSLog(@"数据库打开失败"); }else{ NSLog(@"数据库打开成功"); } //1.准备sqlite语句 /* 不区分大小写 char(长度)。字符串 NULL. 空值 INTEGER. 整型 REAL.浮点型 TEXT.文本类型 BLOB. 二进制类型,用来存储文件,比如图片。 */ //userchat 数据库名字 //(Id char(30), Name char(40), Image blob ) 数据名字+类型 NSString *sqlite = @"create table if not exists userchat(Id char(30), Name char(40), Image blob );"; //2.执行sqlite语句 int result = [dataBase executeUpdate:sqlite]; //3.sqlite语句是否执行成功 if (result == YES) { NSLog(@"创建表成功"); } else { NSLog(@"创建表失败"); } }); return saveDateToLocal; } //插入数据 -(void)SaveDateAddDateWithId:(NSString*)Id ImageviewImage:(UIImage*)Image nickName:(NSString*)Name{ //插入之前 先查询有没有 有了就替换 NSDictionary * dict =[self SaveDatesearchMessageWithId:Id]; if (dict) { //有值 替换/更新 [self SaveDatechangeMessageWithId:Id Name:Name Image:Image]; } else{ NSString *sql = @"insert into userchat(Id , Name, Image) values(?, ?, ?);"; //图片转化为data NSData *data =UIImageJPEGRepresentation(Image, 1.0); NSLog(@"Encoded image:%@", data); //? 在这里 类似OC中的 %@ BOOL ret = [dataBase executeUpdate:sql,Id, Name, data]; if (ret == YES) { NSLog(@"插入成功"); }else{ NSLog(@"插入失败"); } } } -(void)SaveDatechangeMessageWithId:(NSString*)Id Name:(NSString*)Name Image:(UIImage *)Image { //图片转化为data字符串 NSData *data =UIImageJPEGRepresentation(Image, 1.0); // 1、创建sql语句 NSString *sql = @"update userchat set Name =? , Image = ? where Id = ?;"; // 2、执行sql语句 BOOL ret = [dataBase executeUpdate:sql, Name,data,Id]; if (ret == YES) { NSLog(@"修改数据成功"); } } -(NSDictionary*)SaveDatesearchMessageWithId:(NSString*)Id { NSMutableDictionary * dict =[NSMutableDictionary dictionary]; // 1、写sql语句 NSString *sql = @"select *from userchat where Id = ? ;"; FMResultSet *set = [dataBase executeQuery:sql,Id]; //使用while循环 while ([set next]) { //FMResultSet是一个集合,保存所有符合条件的数据 //next是一个自动循环的指针,调用next可以遍历整个集合 //当遍历完成以后,next会指向空,循环退出 //将数据从集合中取出 NSString *name = [set stringForColumn:@"Name"]; if (name.length>0) { NSData * image = [set dataForColumn:@"Image"]; //NSData *decodedImageData = [image dataUsingEncoding:NSUTF8StringEncoding]; UIImage *decodedImage = [UIImage imageWithData:image]; NSDictionary * userchat =@{@"Name":name,@"Image":decodedImage}; [dict setDictionary:userchat]; break ; } } return dict; } -(void)SaveDatedeleteMessageWithId:(NSString*)Id { NSString *sql = @"delete from userchat where Name = ?;"; BOOL ret = [dataBase executeUpdate:sql,Id]; if (ret == YES) { NSLog(@"删除成功"); }else{ NSLog(@"删除shibai"); } } @end