fmdb存储nsdata
如果要存储复杂对象类型的数据, 该对象必须遵循NSCoding协议, 并实现对应代理方法
对象类型实现代理后, 在存储之前还有一部操作, 是把对象进行归档, 归档完成才可以使用FMDB进行存储, 对应类型为BLOB类型,.
如果要直接存储复杂对象, 不想写归档什么的玩意, 可以使用一个第三方框架 JRDB , 这个可以直接存储复杂对象, jrdb是基于FMDB进行的二次封装框架, 可以试试噢
❷ ios fmdb 怎么删除数据库
什么是数据库? 数据库是以某种文件结构存储的一系列信息表,这种文件结构使您能够访问这些表、选择表中的列、对表进行排序以及根据各种标准选择行。数据库通常有多个 索引与这些表中的许多列相关联,所以我们能尽可能快地访问这些表。
❸ FMDB+QRCodeGenerator 图片持久化代码
+(instancetype)sharedData{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
data =[[zhnData alloc]init];
[data initTab];
});
return data;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (data == nil) {
data =[super allocWithZone:zone];
}
return data;
}
-(void)initTab{
NSString *path =(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filepath =[path stringByAppendingString:@"user.db"];
db =[[FMDatabase alloc]initWithPath:filepath];
if ([db open]) {
[db executeUpdate:@"create table image(ids integer primary key autoincrement ,title text, path blob)"];
[db close];
}
}
-(void)addImg:(zhnImage*)image{
[db open];
[db executeUpdate:@"insert into image values (null,?,?)",image.title,image.imagePath];
[db close];
}
-(NSMutableArray*)selectAllData{
[db open];
NSMutableArray *arr =[[NSMutableArray alloc]init];
FMResultSet *result =[db executeQuery:@"select * from image"];
while ([result next]) {
zhnImage *image =[[zhnImage alloc]init];
image.title =[result stringForColumn:@"title"];
image.imagePath =[result dataForColumn:@"path"];
[arr addObject:image];
NSLog(@"查询成功");
}
[db close];
return arr;
}
//然后吧图片放到沙河路径中做持久化
- (IBAction)createBtn:(id)sender {
self.codeImg.image =[QRCodeGenerator qrImageForString:self.titleTF.text imageSize:100];
NSString *path = (NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *imgaePath = [path :[NSString stringWithFormat:@"%@.png",self.titleTF.text]];
NSLog(@"%@",imgaePath);
[[NSFileManager defaultManager] createFileAtPath:imgaePath contents:UIImagePNGRepresentation(self.codeImg.image) attributes:nil];
NSData *imgData =UIImagePNGRepresentation(self.codeImg.image);
zhnImage *image =[[zhnImage alloc]init];
image.title =self.titleTF.text;
image.imagePath =imgData;
[[zhnData sharedData] addImg:image];
}
❹ ios开发fmdb存放nsdata怎么写
NSData-> NSStringNSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];NSString->NSDataNSString *aString = @"1234abcd";NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];2.NSData 与 ByteNSData-> Byte数组NSString *testString = @"1234567890";NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];Byte *testByte = (Byte *)[testData bytes];for(int i=0;i<[testData length];i++)printf("testByte = %d\n",testByte[i]);Byte数组-> NSDataByte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};NSData *adata = [[NSData alloc] initWithBytes:byte length:24];Byte数组->16进制数Byte *bytes = (Byte *)[aData bytes];NSString *hexStr=@"";for(int i=0;i<[encryData length];i++){NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; ///16进制数if([newHexStr length]==1)hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];elsehexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];}NSLog(@"bytes 的16进制数为:%@",hexStr);16进制数->Byte数组///// 将16进制数据转化成Byte 数组NSString *hexString = @"3e435fab9c34891f"; //16进制字符串int j=0;Byte bytes[128]; ///3ds key的Byte 数组, 128位for(int i=0;i<[hexString length];i++){int int_ch; /// 两位16进制数转化后的10进制数unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)int int_ch1;if(hex_char1 >= '0' && hex_char1 <='9')int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48else if(hex_char1 >= 'A' && hex_char1 <='F')int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65elseint_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97i++;unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)int int_ch2;if(hex_char2 >= '0' && hex_char2 <='9')int_ch2 = (hex_char2-48); //// 0 的Ascll - 48else if(hex_char1 >= 'A' && hex_char1 <='F')int_ch2 = hex_char2-55; //// A 的Ascll - 65elseint_ch2 = hex_char2-87; //// a 的Ascll - 97int_ch = int_ch1+int_ch2;NSLog(@"int_ch=%d",int_ch);bytes[j] = int_ch; ///将转化后的数放入Byte数组里j++;}NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];NSLog(@"newData=%@",newData);3. NSData 与 UIImageNSData->UIImageUIImage *aimage = [UIImage imageWithData: imageData];//例:从本地文件沙盒中取图片并转换为NSDataNSString *path = [[NSBundle mainBundle] bundlePath];NSString *name = [NSString stringWithFormat:@"ceshi.png"];NSString *finalPath = [path :name];NSData *imageData = [NSData dataWithContentsOfFile: finalPath];UIImage *aimage = [UIImage imageWithData: imageData];UIImage-> NSDataNSData *imageData = UIImagePNGRepresentation(aimae);