Fetch Image From Sqllite Database in iphone Fetch Image From Sqllite Database in iphone sqlite sqlite

Fetch Image From Sqllite Database in iphone


Your problem is that you cannot insert binary data by building a SQL statement with a stringWithFormat. You're inserting the NSData (binary data) into a field called ImageName, and you cannot use stringWithFormat to build that SQL statement.

Did you really mean to insert the image itself? In that case you'd use SQL like below, with a ? placeholder, and then use sqlite3_bind_blob for column number 1 with your NSData before you call sqlite3_step. Thus, the SQL would look like:

Insert into Gallery(ImageName) VALUES (?)

(As an aside, I'm not a fan of the choice of ImageName for the actual data of the image. I'd either use Image for the actual image data, or ImageName for the name of the image.)

And then, after preparing your SQL, you'd then call:

sqlite3_bind_blob(statement, 1, [dataImage1 bytes], [dataImage1 length], SQLITE_TRANSIENT);

You can then invoke sqlite3_step, like normal, to execute the SQL to insert the data.

When retrieving the data, after you successfully sqlite3_step, you'd use the appropriate sqlite3_column_blob functions:

const void *bytes = sqlite3_column_blob(statement, 0);int len = sqlite3_column_bytes(statement, 0);NSData *data = [NSData dataWithBytes:bytes length:len];UIImage *image = [UIImage imageWithData:data];

Having said that, SQLite is notoriously inefficient in storing large blob data. You might want to store the image in the Documents folder and then just store the filename in your ImageName field.


i think you are getting bytes in string type in

[[arra objectAtIndex:0] valueForKey:@"ImageName"];

so first get string and then convert it to data.

try like this:

NSData* data = [[[arra objectAtIndex:0] valueForKey:@"ImageName"] dataUsingEncoding:NSUTF8StringEncoding];