如何更新ios中的sqlite数据库中的图像?

时间:2015-10-14 10:37:05

标签: ios objective-c sqlite

我可以用 BLOB 格式将图像插入 sqlite 数据库,同时更新图像,在sqlite数据库中存储 nsdata

这是更新查询:

NSString* updateQuery = [NSString stringWithFormat:@"UPDATE MediaStorage SET media_content = '%@',name = '%@',type = '%@', extension = '%@',file_id = '%@',exp_id = '%@' WHERE ID = '%ld'",prjct.profileData,prjct.imgName,prjct.imgType,prjct.imgExtension,prjct.imgId,prjct.exp_id,(long)[prjct.imgId integerValue]];

1 个答案:

答案 0 :(得分:0)

sqlite3 *database;     
sqlite3_stmt *Stmt;

sqlite3_open([[self getDBPath] UTF8String],&database);

- (NSString *) getDBPath
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:****.sqlite];
}

NSString* updateQuery = [NSString stringWithFormat:@"UPDATE MediaStorage SET media_content = ?,name = '%@',type = '%@', extension = '%@',file_id = '%@',exp_id = '%@' WHERE ID = '%ld'",prjct.imgName,prjct.imgType,prjct.imgExtension,prjct.imgId,prjct.exp_id,(long)[prjct.imgId integerValue]];

 if(sqlite3_prepare_v2(database, [updateQuery UTF8String], -1, &Stmt, NULL) == SQLITE_OK)
     {
         sqlite3_bind_blob(Stmt, 1,[prjct.profileData bytes], [prjct.profileData length], NULL);
         sqlite3_step(Stmt);
         sqlite3_reset(Stmt);
         if(Stmt)
             sqlite3_finalize(Stmt);
     }