网上说的方法
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename = [doc stringByAppendingPathComponent:@"creiNO.sqlite"];
// 2.得到数据库
self.db = [FMDatabase databaseWithPath:filename];
[self changBiao];
// 4.创表
-(void)changBiao{
if ([self.db open]) {
// @"create table if not exists refCar (bdid integer primary key, shopin text, date integer, salary real)
BOOL result = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS sNOcar (dbid integer PRIMARY KEY AUTOINCREMENT, crnid TEXT NOT NULL, imagedata text);"];//网上是blob
if (result) {
NSLog(@"igggd成功创表");
} else {
NSLog(@"创表失败");
}
}
[self.db close];
//
[self inist];
}
-(void)inist{
//NSData * date = [[NSData alloc]init];
//UIImage * image = [UIImage imageNamed:@"share1"];
NSString * paht = [[NSBundle mainBundle]pathForResource:@"share1" ofType:@"jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:paht];
// NSLog(@"3333 %@", image);
//第一步
//第二种 直接变成data blob类型
:1 NSData * date = UIImageJPEGRepresentation(image, 1);
// NSString * st = [date base64Encoding];
// NSData * st =[date base64EncodedDataWithOptions:1];
//第一种
//第二步
NSString * st = [date base64EncodedStringWithOptions:1];
self.daeta = date;
if ([self.db open]) {
NSString *insertSql1= [NSString stringWithFormat:@"INSERT INTO sNOcar (crnid,imagedata) VALUES ('%@', '%@')",@"33" , st];
BOOL res = [self.db executeUpdate:insertSql1];
第二种插入时
这么插入 ?什么类型都可以
BOOL res = [self.db executeUpdate:@"INSERT INTO sNOcar (crnid,imagedata) VALUES ( ?, ?)",@"33" , date];
if (res) {
NSLog(@"日志数据插入表成功");
} else {
NSLog(@"日志数据插入表失败");
}
}
[self.db close];
}
//查询
-(void)selectshuj{
UIImage * imaged;
if ([self.db open]) {
//查询某个数据where crnNO = '0020170222'
NSString * sql = @"SELECT * FROM sNOcar ";
FMResultSet * rs = [self.db executeQuery:sql];
while ([rs next]) {
// int dbId = [rs intForColumn:@"dbid"];
NSString * crnid = [rs stringForColumn:@"crnid"];
//第二种
// NSData * date = [rs dataForColumn:@"imagedata"];
//第一种
.//取出数据
NSString * date = [rs stringForColumn:@"imagedata"];
NSData *decodedImageData = [[NSData alloc]initWithBase64EncodedString:date options:1];
// NSData * daterrr =[[NSData alloc]initWithBase64EncodedData:date options: 1];
// NSData *decodedImageData = [[NSData alloc]initWithBase64EncodedString:date options:1];
// NSData *decodedImageData = [[NSData alloc] initWithBase64Encoding:date];
NSData *decodedImageData=[[NSData alloc]initWithBase64EncodedData:date options:1];
// NSData * datef = [[NSData alloc]initWithBytes:date.bytes length:date.length];
NSLog(@"---------------------------------加载 66h %@ ", crnid);
// NSLog(@"---------444444444-----加载hhhhhhhhhhh %@ ", date );
// NSLog(@"---------------------------------加载hhhhhhhhhhh %@ ", datef );
NSLog(@"------------3333333---------------------加载hhhhhhhhhhh %@ ", decodedImageData );
// UIImage * image = [UIImage imageWithData:datef];
// NSData * date = [rs dataForColumn:<#(NSString *)#>]
//转换成照片
self.imagef = [[UIImage alloc]initWithData:decodedImageData];
// imaged = image;
NSLog(@"---------------------------加载ttttt %@ ", self.imagef);
}
对于网上说的存blob类型 取出来时 转换成照片 失败 为空 不知道为什么
}
[self.db close];
self.imgview.image = self.imagef;
// NSLog(@" %@",self.arrraymodel);
}