1

coding in .m

-(IBAction) getPhoto:(id) sender {
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

} else {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                message:@"You don't have a camera for this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        //shows above alert if there's no camera
        [noCameraAlert show];
    }else{
        picker.sourceType    = UIImagePickerControllerSourceTypeCamera;
        picker.allowsEditing = YES;
        picker.delegate      = self;
    }
}
[self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     UIImage *img     = [info objectForKey:UIImagePickerControllerOriginalImage];
img              = [info valueForKey:UIImagePickerControllerEditedImage];
self.imageView.image  = img;
imagData         = UIImagePNGRepresentation(img);
[picker dismissViewControllerAnimated:YES completion:nil];
}

the image was placed in Image view now how can i save the image in database

I'm already having the database to insert the data To Insert:

NSMutableArray *listItemsarr = [[NSMutableArray alloc] init];
        if([listItemsarr count]==0){
            NSString *insertQry=@"INSERT INTO selection_tbl (expenses_value,category,date,payment,description) VALUES(?,?,?,?,?)";
            sqlite3_stmt *stmt=[DB OpenSQL:[insertQry UTF8String]];
            if(stmt !=nil)
            {
                sqlite3_bind_text(stmt,1, [dataTextfield.text UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,2, [[selectionarray objectAtIndex:0]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,3, [[selectionarray objectAtIndex:1]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,4, [[selectionarray objectAtIndex:2]  UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_bind_text(stmt,5, [[selectionarray objectAtIndex:3]  UTF8String], -1, SQLITE_TRANSIENT);


                sqlite3_step(stmt);
            }
            sqlite3_finalize(stmt);
            [DB CloseSQL];

The following coding is use to update the database values To Update:

NSInteger proeditid = [primarykeyID integerValue];
        NSString *updateQry=@"update selection_tbl set expenses_value=?,category=?,date=?,payment=?,description=? where expenses_id=?";
        sqlite3_stmt *stmt=[DB OpenSQL:[updateQry UTF8String]];
        if(stmt !=nil)
        {
            sqlite3_bind_text(stmt,1, [dataTextfield.text UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,2, [[selectionarray objectAtIndex:0] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,3, [[selectionarray objectAtIndex:1] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,4, [[selectionarray objectAtIndex:2] UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt,5, [[selectionarray objectAtIndex:3] UTF8String], -1, SQLITE_TRANSIENT);


            sqlite3_bind_int(stmt,6, (int)proeditid);
            sqlite3_step(stmt);
        }
        sqlite3_finalize(stmt);
        [DB CloseSQL];
  • but can you please tell me how to add Image which was taken by UIImagePickerControllerSourceTypeCamera into database alone? –  Mar 09 '15 at 05:55
  • By the way, three snaps for proper use of the `sqlite3_bind_xxx` functions. So many people incorrectly use `stringWithFormat` to build their SQL, so it's nice to see someone properly binding values to `?` placeholders. Well done. – Rob Mar 09 '15 at 06:04

2 Answers2

1

There are two questions:

  1. How to get the NSData representation of the image?

  2. How to save the NSData representation in a SQLite database?

    To save this NSData in your database, you then use sqlite3_bind_blob. The process is very similar to the sqlite3_bind_xxx functions that you shared with us earlier. Use the bytes method of NSData to access the C-style pointer to the buffer, and pass that to sqlite3_bind_blob.

Note, saving full size images in a SQLite database is notoriously inefficient. SQLite doesn't handle large blobs well. Generally people would prefer to save the NSData into a file in the Documents folder on the device's persistent storage, and then just save the path of that file in the database.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • check out my question now hope it will be easy to understand now –  Mar 09 '15 at 06:04
  • Putting code in the question rather than comments is much better. Thanks. I hope my answer is equally clear: First get `NSData` representation of the image (using either of those two techniques I discuss above), and then use `bytes` and `length` methods of the resulting `NSData` and pass those to the `sqlite3_bind_blob` function. – Rob Mar 09 '15 at 06:06
  • sure sir will do it soon Thank you so much for your reply @Rob –  Mar 09 '15 at 06:08
0

Add the following line in your code which will add the image in your database

 sqlite3_bind_blob(stmt,6, [imagData bytes], (unsigned)[imagData length], SQLITE_TRANSIENT);

Final code will looks like this:

NSMutableArray *listItemsarr = [[NSMutableArray alloc] init];

            if([listItemsarr count]==0){
                NSString *insertQry=@"INSERT INTO selection_tbl (expenses_value,category,date,payment,description,image) VALUES(?,?,?,?,?,?)";
                sqlite3_stmt *stmt=[DB OpenSQL:[insertQry UTF8String]];
                if(stmt !=nil)
                {
                    sqlite3_bind_text(stmt,1, [dataTextfield.text UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,2, [[selectionarray objectAtIndex:0]  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,3, [dateString  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,4, [[selectionarray objectAtIndex:2]  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt,5, [[selectionarray objectAtIndex:3]  UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_blob(stmt,6, [imagData bytes], (unsigned)[imagData length], SQLITE_TRANSIENT);

                    sqlite3_step(stmt);
                }
                sqlite3_finalize(stmt);
                [DB CloseSQL];