Reputation: 3928
In my application I stored multiple images
in my database
.
Where my database field type is blob.
Storing the image
data works well but when i get image
data
from database using below code
NSData *photodata=[[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStmt, 12) length:sqlite3_column_bytes(compiledStmt, 12)];
But the field contains null data it returns me <3c3e> in place of null value.
And I inserted data using below query
strQuery_DB = [NSString stringWithFormat:@"update tbl_project_review set answer ='%@',comment ='%@',photodata='%@' where prjid=%d",temp,escapedStr1,tempdata,prjid];
Upvotes: 2
Views: 219
Reputation: 95325
When you use the %@
specifier with the stringWithFormat:
method, it invokes the description
method of the respective argument. For NSData
objects, this returns a hexadecimal representation of the data surrounded by <
and >
. For an empty NSData
object it will return the string <>
and this is what is being inserted into the database. This is probably not what you want to do, since this will also double the size of your image data unnecessarily.
You should use parameterised query functions such as sqlite_bind_blob
. This will make it easier to deal with data input. Basically, you need to change your update query to something like the following (add appropriate error checking, etc):
const char sql[] = "update tbl_project_review set answer = ?, comment = ?, photodata = ? where prjid = ?";
sqlite3_stmt *stmt = NULL;
sqlite3_prepare_v2(db, sql, sizeof sql, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [tmp UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [escapedStr1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(stmt, 3, [tempdata bytes], [tempdata length], SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 4, prjid);
sqlite3_step(stmt); // check for SQLITE_DONE
Upvotes: 2
Reputation: 20541
bellow is simple code for retrive image from database try..
NSString *imgurl=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
int length = sqlite3_column_bytes(compiledStatement, 4);
// NSData *data = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 4) length:length];
NSString *imageString=[NSString stringWithCharacters:sqlite3_column_blob(compiledStatement, 4) length:length];
UIImage *personImage=[UIImage imageNamed:imageString];
i hope this help you...
Upvotes: 0
Reputation: 6323
Storing image as blob in DB is bad procedure. you need to save image in your cache directory and then you need to save path of image in DB is good.
Upvotes: 2