5

im Working on inserting IMAGE in to Database using sqlite in Xcode 4.6 ,i just wanted to insert a textfield and uiimageview(having dynamically generated barcode image).

unfortunately im getting " Error is: out of memory "

hear is the code I'm using ,plz let me know where my mistake is & Let me know for more details

* 1. Database creation*

-(void)createoropendb {

    NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docpath =[path objectAtIndex:0];

    dbpathstring =[docpath stringByAppendingPathComponent:@"DataBase.db"];
    char *error;

    NSFileManager *filemanager =[NSFileManager defaultManager];

    if (![filemanager fileExistsAtPath:dbpathstring])
    {
        const char *dbpath =[dbpathstring UTF8String];

        if (sqlite3_open(dbpath, &barcodeDB) == SQLITE_OK)
        {
            const char *sql_start ="CREATE TABLE IF NOT EXISTS DBTABLE1(NAME TEXT UNIQUE,IMAGEURL BLOB)";
             NSLog(@"db created");
            sqlite3_exec(barcodeDB, sql_start,NULL,NULL, &error);
            sqlite3_close(barcodeDB);

        }
    }
}

* 2.button to insert data*

- (IBAction)savedata:(id)sender  {
    UIImage *image =imageView.image;
    NSData *imageData=UIImagePNGRepresentation(image);   
    [self SaveImagesToSql: imageData.bytes:Uimagetext.text];  
}

* 3.method to insert data*

- (void) SaveImagesToSql: (NSData*) imgData :(NSString*) mainUrl {   

    const char* sqliteQuery = "INSERT INTO DBTABLE1(NAME,IMAGEURL) VALUES (?, ?)";
    sqlite3_stmt* statement;

    if( sqlite3_prepare_v2(barcodeDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
    {
        sqlite3_bind_text(statement, 1,[mainUrl UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_blob(statement, 2,[imgData bytes],[imgData length], SQLITE_TRANSIENT);

        sqlite3_step(statement);

        NSLog( @"Saved image successfully" );

    }
    else
    {


      NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s",sqlite3_errmsg(barcodeDB) );
    }
    // Finalize and close database.
    sqlite3_finalize(statement);
}
4
  • By the way, while I hope I answered your question below, but two unrelated observations: 1. I should point out that storing images in the database doesn't always work very well. If you're dealing with large images, you'll often get much better performance storing the image in the Documents folder of the file system, and only saving the filename in the database. If you're dealing with thumbnails, the difference is modest, but with large images, the performance hit is observable. Commented Mar 27, 2013 at 4:22
  • 1
    2. I don't know where you're getting the images (from the network?) but retrieving the images via UIImagePNGRepresentation is not guaranteed to get you the same file as you originally retrieved when you got the image. You might want to save the image as you're updating the UIImageView, not retrieving the UIImage later and converting it back to a NSData with UIImagePNGRepresentation. PNG is less problematic than JPG, but it's still not perfect. Save the original data and bypass the round-trip to the UIImage property if you can. Commented Mar 27, 2013 at 4:25
  • 3. You should also check the results of sqlite3_step (in case the insert failed). Probably sqlite3_bind_... statements, too, though I've never had them fail on me. Commented Mar 27, 2013 at 4:35
  • 1
    Rob,I understand what you said earlier to open database after sqlite3_prepare,I'm working on it & currently i'm not getting images from server i'm generating them in program itself. k Rob i will check the sqlite3_bind_.. statements too now Commented Mar 27, 2013 at 4:36

1 Answer 1

1

The return code (which is not SQLITE_OK) is undoubtedly returning SQLITE_MISUSE, meaning that you're trying to use a database that you haven't opened yet. Your createoropendb is creating and opening if the database doesn't exist, but if it does exist, you're not opening it (and even if it did exist, you're closing it after creating it).

Bottom line, if the database is not opened when you call sqlite3_prepare, that function will return SQLITE_MISUSE, any calls to sqlite3_errmsg will return a misleading "Out of memory" text message.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Rob,nice to see your response buddy,i have a small doubt, my database is successfully created.so where is my problem now is it trying to insert in unknown database.
@CodeWorrior If you look at your createoropendb, what will it do if the database already exists? Nothing. It does not open the database if it already exists. And if it created it, it's immediately closing it, too. That's a problem. You have to do sqlite3_open. And if you try to insert into a database without doing sqlite3_open, you'll get that cryptic "Out of memory" error.
@CodeWorrior That looks much better. I'd suggest you also check the result of sqlite3_step, e.g. if (sqlite3_step(statement) == SQLITE_DONE) NSLog( @"Saved image successfully" ); else NSLog( @"SaveBody: Failed from sqlite3_step. Error is: %s", sqlite3_errmsg(barcodeDB));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.