Hi all,
I have an issue with sqlite database.
I have set up a SQLite DB that currently reads and writes
I have an issue with sqlite database.
I have set up a SQLite DB that currently reads and writes
NSString
s perfectly. I also want to store an image in the database and recall it later.
My current process goes like this:
UIImagePickerController
-> User Chooses Image from Photos -> chosenImage is set to instance of UIImageView
-> Now I want to take this image and store it in the DB.
I've read up a bit on using
NSData
and encoding the image,Now I am able to insert my Image to Database.
But while retrieving from database I am writing like this.
NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 5) length: sqlite3_column_bytes(statement, 5)];
if(dataForCachedImage == nil)
{
NSLog(@"No image found.");
}
else
{
self.image = [UIImage imageWithData:dataForCachedImage];
NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(database) );
}
[dataForCachedImage release];
Here I am getting the data in dataForCachedImage,but
self.image = [UIImage imageWithData:dataForCachedImage];
after this it is showing nil object in image.
Can anyone help me out.
Thanks
Tejaswini.S
For Inserting image data:
ReplyDeleteUIImage *image = ImageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
sqlite3_bind_blob(statement, 4, [imageData bytes], [imageData length], NULL);
For retrieving the image data:
dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:@"Vaccinations.sqlite"];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select ImageView from ChildData";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, nil) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
int length = sqlite3_column_bytes(selectstmt, 3);
NSData *data = [NSData dataWithBytes:sqlite3_column_blob(selectstmt, 3) length:length];
UIImage *savedImage = [UIImage imageWithData:data];
[dictionary setValue:savedImage forKey:@"ChildImage"];
}
}
sqlite3_finalize(selectstmt);
sqlite3_close(database);
}
Note: don't release the object before complete the accessing of values. please check it once. release the object after assigning the values.
Thanks & regards,
R.Anusha
Thanks Anusha .Yes, actually my problem is while inserting itself I didn't bind the data instead I wrote sqlite3_execute statement.Thats why I am getting the data but not able to retrieve image back.
Delete