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);