0

How to insert image in database, I tried to but its not inserting. Where is the problem in query? This is my model class .m file: code in this file after sqlite3_prepare_v2 my trace is not inserting in sqlite3_bind_blob it is going directly outside after sqlite3_prepare_v2. What is the problem here, please help me. Something is wrong here for saving the data. In model class i create UIImage* Photo; and make protocol and sys

+(void)SaveImagesToSql:(NSData *)imgDat :(int)getid
{
    NSString *filePath = [BaseModal getDBPath];
    sqlite3 *database;
    sqlite3_stmt *updStmt;
    NSLog(@"%@",filePath);
    if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) 
    { 
        NSString* sqliteQuery = [NSString stringWithFormat:@"UPDATE INTO Peoples (Photo) VALUES (?) where PeopleId =%i",getid];

        if(sqlite3_prepare_v2(database, [sqliteQuery UTF8String] , -1, &updStmt, NULL) == SQLITE_OK)    {

            sqlite3_bind_blob(updStmt, 1, [imgDat bytes], [imgDat length], NULL);

        }
        sqlite3_finalize(updStmt);
    }
    sqlite3_close(database);
}

this is my view.m file i am choosing image from this page and insert in to sqlite file

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //UIImageWriteToSavedPhotosAlbum(imageView.image, self, nil, nil);

    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *docDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@", docDirectory];
    NSLog(@"thiss%@",filePath);
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {    
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   
        [picker dismissModalViewControllerAnimated:YES];
        return;
    } 


    [picker release];
    UIImage *image1 =imageView.image;
    NSData *myData = UIImagePNGRepresentation(image1);

    [PeopleModal SaveImagesToSql:myData :peopleid]; 

    //[self.parentViewController dismissModalViewControllerAnimated:YES];

    [picker dismissModalViewControllerAnimated:YES];



}
Vidya Murthy
  • 530
  • 1
  • 8
  • 22
Rocky
  • 1,427
  • 1
  • 28
  • 65

1 Answers1

3

You should not insert blobs in db, this will make your db very very slow!!

You should save the image in documents directory of the app and in the db save only the path of the image.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71