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

这是更新查询:

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

最佳答案

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

09-11 19:15