0

I am try to fetch image from Gallery and save it to SQLite Database. so that i will use this image in other part of my Application. I am get image from Gallery and show it in imageView it works fine but when i try to save it in Database application crash. I am using following code to convert image in NSData and then save it to Database field which is BLOB in type.

NSData * imageData;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    [picker dismissModalViewControllerAnimated:YES];

    ImageView.image= img;

    imageData= UIImagePNGRepresentation(_ImageView.image);
}

and My SQLite insert method is:

-(void) CreateGoal:(NSString *)Title :(NSString *) Description :(NSString *)NextStep :(NSString *) Date :(NSData *)image{

    //Get list of directories in Document path
    NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //Define new path for database
    NSString * documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"GoalBook.db"];


    if(!(sqlite3_open([documentPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured.");
        return;
    }else{
        NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO createGoal(title, description, nextstep, date, image) VALUES ('%@','%@','%@', '%@', '%@')",
                               Title, Description, NextStep, Date, image];

        const char *sql = [insertSQL UTF8String];

        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement");
            return;
        }else{

            if(sqlite3_step(sqlStatement)==SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(db);
            }
        }

    }

}

I use use this method for save my Data like this:

[self.database CreateGoal:_Goaltitle.text :_goaldescription.text :_goalnext.text :getGoaldate :imageData];

I googled a lot to find an answer, but after experimenting a lot I gave up.

Can any one suggest what is wrong with this Please ?

Thanks in advance

Guri
  • 85
  • 1
  • 1
  • 9
  • 1
    Saving image in database is not a good idea. – Midhun MP Dec 23 '13 at 08:19
  • Agree with @MidhunMP. Save only meta-informations there (e.g. the path). – Lorenzo B Dec 23 '13 at 08:26
  • Thanks Midhun MP for reply but how can i get complete path with name of image ? can you please give me a link of tutorials regarding this. – Guri Dec 23 '13 at 08:27
  • @user3128821: You can store the [asset url to database](http://stackoverflow.com/questions/14378599/how-to-save-path-of-image-picked-from-uiimagepickerviewcontroller-to-sqlite-data) and use that [url to display image](http://stackoverflow.com/questions/3837115/display-image-from-url-retrieved-from-alasset-in-iphone) – Midhun MP Dec 23 '13 at 09:49

0 Answers0